Java Matrix Convert To matrixToArray(int[][] input, int fromRow, int rows, int fromColumn, int columns)

Here you can find the source of matrixToArray(int[][] input, int fromRow, int rows, int fromColumn, int columns)

Description

Converts a 2 dimensional array into a single dimension.

License

Open Source License

Parameter

Parameter Description
input a parameter
fromRow a parameter
rows a parameter
fromColumn a parameter
colums a parameter

Return

Single dimensional array containing the required data

Declaration

public static int[] matrixToArray(int[][] input, int fromRow, int rows, int fromColumn, int columns) 

Method Source Code

//package com.java2s;
/*//from  ww w  .ja  v  a  2s . c o m
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a 2 dimensional array into a single dimension.
     * Places each column on top of each other.
     * Provides controllers for selecting a subset of rows or columns. 
     * 
     * @param input
     * @param fromRow
     * @param rows
     * @param fromColumn
     * @param colums
     * @return Single dimensional array containing the required data
     */
    public static int[] matrixToArray(int[][] input, int fromRow, int rows, int fromColumn, int columns) {
        int[] output = new int[rows * columns];
        for (int c = 0; c < columns; c++) {
            for (int r = 0; r < rows; r++) {
                output[c * rows + r] = input[r + fromRow][c + fromColumn];
            }
        }
        return output;
    }

    /**
     * Converts a 2 dimensional array into a single dimension.
     * Places each column on top of each other.
     * Provides controllers for selecting a subset of rows only. 
     * 
     * @param input
     * @param fromRow
     * @param rows
     * @return Single dimensional array containing the required data
     */
    public static int[] matrixToArray(int[][] input, int fromRow, int rows) {
        return matrixToArray(input, fromRow, rows, 0, input[0].length);
    }

    /**
     * Converts a 2 dimensional array into a single dimension.
     * Places each column on top of each other.
     * 
     * @param input
     * @return Single dimensional array containing the required data
     */
    public static int[] matrixToArray(int[][] input) {
        return matrixToArray(input, 0, input.length, 0, input[0].length);
    }

    /**
     * Converts a 2 dimensional array into a single dimension.
     * Places each column on top of each other.
     * Provides controllers for selecting a subset of rows or columns. 
     * 
     * @param input
     * @param fromRow
     * @param rows
     * @param fromColumn
     * @param colums
     * @return Single dimensional array containing the required data
     */
    public static double[] matrixToArray(double[][] input, int fromRow, int rows, int fromColumn, int columns) {
        double[] output = new double[rows * columns];
        for (int c = 0; c < columns; c++) {
            for (int r = 0; r < rows; r++) {
                output[c * rows + r] = input[r + fromRow][c + fromColumn];
            }
        }
        return output;
    }

    /**
     * Converts a 2 dimensional array into a single dimension.
     * Places each column on top of each other.
     * Provides controllers for selecting a subset of rows only. 
     * 
     * @param input
     * @param fromRow
     * @param rows
     * @return Single dimensional array containing the required data
     */
    public static double[] matrixToArray(double[][] input, int fromRow, int rows) {
        return matrixToArray(input, fromRow, rows, 0, input[0].length);
    }

    /**
     * Converts a 2 dimensional array into a single dimension.
     * Places each column on top of each other.
     * 
     * @param input
     * @return Single dimensional array containing the required data
     */
    public static double[] matrixToArray(double[][] input) {
        return matrixToArray(input, 0, input.length, 0, input[0].length);
    }
}

Related

  1. matrixCastToDouble(String[][] m)
  2. matrixToArray(double[][] m)
  3. matrixToQuaternion(float[] m)
  4. matrixToShort(final byte[][] b)
  5. toDiagonalMatrix(double[] x)