Java Matrix to Vector matrixToVectorArray(int M, int N, float[][] a, float[] b)

Here you can find the source of matrixToVectorArray(int M, int N, float[][] a, float[] b)

Description

Converts a 2D array into a 1D array leaving the result in b .

License

Apache License

Parameter

Parameter Description
M number of data points in x direction
N number of data points in y direction
a 2D array
b 1D array

Declaration

public static void matrixToVectorArray(int M, int N, float[][] a, float[] b) 

Method Source Code

//package com.java2s;
/*//from w ww .  jav  a 2s  .  com
 * Copyright 2016 Universidad Nacional de Colombia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Converts a 2D array into a 1D array leaving the result in {@code b}. The
     * information on the 1D array is distributed as the rows of the 2D array in
     * sequence.
     *
     * @param M number of data points in x direction
     * @param N number of data points in y direction
     * @param a 2D array
     * @param b 1D array
     */
    public static void matrixToVectorArray(int M, int N, float[][] a, float[] b) {
        if (b.length == 0 || a.length == 0 || a[0].length == 0 || b.length != (M * N) || a.length != M
                || a[0].length != N) {
            throw new IllegalArgumentException(
                    "The number of data points in both arrays must be equal and different from zero.");
        }

        for (int i = 0; i < M; i++) {
            System.arraycopy(a[i], 0, b, N * i, N);
        }
    }

    /**
     * Converts a 2D array into a 1D array leaving the result in {@code b}. The
     * information on the 1D array is distributed as the rows of the 2D array in
     * sequence.
     *
     * @param M number of data points in x direction
     * @param N number of data points in y direction
     * @param a 2D array
     * @param b 1D array
     */
    public static void matrixToVectorArray(int M, int N, double[][] a, double[] b) {
        if (b.length == 0 || a.length == 0 || a[0].length == 0 || b.length != (M * N) || a.length != M
                || a[0].length != N) {
            throw new IllegalArgumentException(
                    "The number of data points in both arrays must be equal and different from zero.");
        }

        for (int i = 0; i < M; i++) {
            System.arraycopy(a[i], 0, b, N * i, N);
        }
    }
}

Related

  1. matrix2Vector(double[][] m)
  2. matrix2vector(int[][] matrix)
  3. matrixToVectorArray(int M, int N, float[][] a, float[] b)
  4. matrixVector(double[][] w, double[] v)
  5. matrixVectorProductVDw(final double[][] V, final double[] d, final double[] w, final double[] a, int l1, int l2)