Java Array Concatenate concatenate(float[][] a, float[][] b, int dim)

Here you can find the source of concatenate(float[][] a, float[][] b, int dim)

Description

concatenate

License

Apache License

Declaration

public static float[][] concatenate(float[][] a, float[][] b, int dim) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.List;

public class Main {
    public static float[] concatenate(List<float[]> list) {
        int totlen = 0;
        for (int i = 0; i < list.size(); i++) {
            totlen += list.get(i).length;
        }//from   w w w.ja va  2s. c  o  m
        float[] out = new float[totlen];
        int k = 0;
        for (int i = 0; i < list.size(); i++) {
            float[] a = list.get(i);
            for (int j = 0; j < a.length; j++) {
                out[k] = a[j];
                k++;
            }
        }
        return out;
    }

    public static float[][] concatenate(float[][] a, float[][] b, int dim) {
        float[][] out = null;
        if (dim == 0) {
            if (a[0].length != b[0].length) {
                System.out.println("number of columns of inputs are not the identical");
                return null;
            }
            out = new float[a.length + b.length][a[0].length];

            int k = 0;
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    out[i][j] = a[i][j];
                }
                k++;
            }
            for (int i = 0; i < b.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    out[k][j] = b[i][j];
                }
                k++;
            }

        } else {
            if (a.length != b.length) {
                System.out.println("number of rows of inputs are not the identical");
                return null;
            }
            out = new float[a.length][a[0].length + b[0].length];

            int k = 0;
            for (int j = 0; j < a[0].length; j++) {
                for (int i = 0; i < a.length; i++) {
                    out[i][j] = a[i][j];
                }
                k++;
            }
            for (int j = 0; j < b[0].length; j++) {
                for (int i = 0; i < b.length; i++) {
                    out[i][k] = b[i][j];
                }
                k++;
            }

        }
        return out;
    }

    public static double[][] concatenate(List<double[][]> alist, int dim) {
        double[][] out = null;
        if (dim == 0) { // concat rows
            int N = 0;
            int M = 0;
            for (double[][] l : alist) {
                N += l.length;
                if (l[0].length > M) {
                    M = l[0].length;
                }
            }
            out = new double[N][M];

            int k = 0;
            for (double[][] l : alist) {
                for (int i = 0; i < l.length; i++)
                    for (int j = 0; j < l[i].length; j++) {
                        out[k][j] = l[i][j];
                        k++;
                    }
            }

        } else { // concat cols
            int N = 0;
            int M = 0;
            for (double[][] l : alist) {
                N += l[0].length;
                if (l.length > M) {
                    M = l.length;
                }
            }
            out = new double[M][N];
            int k = 0;
            for (double[][] l : alist) {
                for (int i = 0; i < l.length; i++)
                    for (int j = 0; j < l[i].length; j++) {
                        out[i][k] = l[i][j];
                        k++;
                    }
            }
        }

        return out;
    }
}

Related

  1. concatArrays(T[] first, T[] second)
  2. concatByteArrays(final byte[] array1, final byte[] array2)
  3. concatenate(@SuppressWarnings("unchecked") T[]... arrays)
  4. concatenate(byte[] a, byte[] b)
  5. concatenate(double[] p1, double[] p2)
  6. concatenate(int[]... arrays)
  7. concatenate(Object[] array)
  8. concatenate(Object[] array)
  9. concatenate(Object[] array)