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

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

Description

Converts a 1D array into a 2D 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 1D array
b 2D array

Declaration

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

Method Source Code

//package com.java2s;
/*//from ww  w .ja  v  a  2s  .  c  o  m
 * 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 1D array into a 2D array leaving the result in {@code b}. It
     * is assumed that 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 1D array
     * @param b 2D array
     */
    public static void vectorToMatrixArray(int M, int N, float[] a, float[][] b) {
        if (a.length == 0 || b.length == 0 || b[0].length == 0 || a.length != (M * N) || b.length != M
                || b[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, N * i, b[i], 0, N);
        }
    }

    /**
     * Converts a 1D array into a 2D array leaving the result in {@code b}. It
     * is assumed that 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 1D array
     * @param b 2D array
     */
    public static void vectorToMatrixArray(int M, int N, double[] a, double[][] b) {
        if (a.length == 0 || b.length == 0 || b[0].length == 0 || a.length != (M * N) || b.length != M
                || b[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, N * i, b[i], 0, N);
        }
    }
}

Related

  1. toIntArray(Vector vect)
  2. toPercent(Vector members, HashMap mapMemberToValue)
  3. vector2doubles(Vector _v)
  4. vectorToColor(float x, float y, float z)
  5. vectorTolatLonDegrees(double X, double Y, double Z)