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

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

Description

flatten

License

Open Source License

Declaration

public static double[] flatten(double[][] array) 

Method Source Code

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

public class Main {
    public static double[] flatten(double[][] array) {
        //check parameter..
        for (int i = 1; i < array.length; ++i) {
            if (array[i].length > array[0].length) {
                throw new IllegalArgumentException(String.format(
                        "Subarray has too many elements (%d). Must be at most array[0].length (%d).%n",
                        array[i].length, array[0].length));
            }/* ww w  .ja  v  a  2  s  .  c om*/
        } //end of check

        double[] res = new double[array.length * array[0].length];
        for (int i = 0; i < res.length; i++) {
            res[i] = array[i / array[0].length][i % array[0].length];
        }
        return res;
    }

    public static boolean[] flatten(boolean[][] array) {
        //check parameter..
        for (int i = 1; i < array.length; ++i) {
            if (array[i].length > array[0].length) {
                throw new IllegalArgumentException(String.format(
                        "Subarray has too many elements (%d). Must be at most array[0].length (%d).%n",
                        array[i].length, array[0].length));
            }
        } //end of check

        boolean[] res = new boolean[array.length * array[0].length];
        for (int i = 0; i < res.length; i++) {
            res[i] = array[i / array[0].length][i % array[0].length];
        }
        return res;
    }
}

Related

  1. flatten(boolean[][] array)
  2. flatten(byte[][] matrix)
  3. flatten(double[][] arr)
  4. flatten(double[][] array)
  5. flatten(double[][] matrix)
  6. flatten(double[][] matrix)
  7. flatten(double[][] spills)
  8. flatten(int[][] arr)