Java Matrix Flatten flatten(double[][] matrix)

Here you can find the source of flatten(double[][] matrix)

Description

Flatten an array.

License

Open Source License

Parameter

Parameter Description
matrix a matrix of doubles

Return

the linearized given matrix

Declaration

public static Double[] flatten(double[][] matrix) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w  w w.j  a  v a  2  s  . co m
     * Flatten an array.
     * @param matrix a matrix of doubles
     * @return the linearized given matrix
     */
    public static Double[] flatten(double[][] matrix) {
        int linearLength = 0;
        // Determine the length of the linearized array
        for (double[] array : matrix) {
            linearLength += array.length;
        }

        Double[] linear = new Double[linearLength];

        // array index
        int i = 0;
        for (double[] array : matrix) {
            for (double el : array) {
                // Assign the element and update the index
                linear[i] = el;
                i++;
            }
        }

        return linear;
    }
}

Related

  1. flatten(byte[][] matrix)
  2. flatten(double[][] arr)
  3. flatten(double[][] array)
  4. flatten(double[][] array)
  5. flatten(double[][] matrix)
  6. flatten(double[][] spills)
  7. flatten(int[][] arr)
  8. Flatten(int[][] in, int[] out)
  9. flatten(String[][] table)