Return a new matrix with the columns of matrix1 joined on the back of matrix2 - Java java.lang

Java examples for java.lang:Math Matrix

Description

Return a new matrix with the columns of matrix1 joined on the back of matrix2

Demo Code

/*//from www. j  av a  2  s . c o m
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    /**
     * Return a new matrix with the columns of matrix1 joined on the back of matrix2
     * 
     * @param matrix1
     * @param matrix2
     * @return
     * @throws Exception 
     */
    public static double[][] appendColumns(double[][] matrix1,
            double[][] matrix2) throws Exception {
        double[][] data = new double[matrix1.length][];

        if (matrix1.length != matrix2.length) {
            throw new Exception(
                    "matrix1 and matrix2 have different lengths");
        }
        if (matrix1.length == 0) {
            return data;
        }
        for (int r = 0; r < matrix1.length; r++) {
            data[r] = append(matrix1[r], matrix2[r]);
        }

        return data;
    }

    /**
     * Append the vector u to the vector v and return the result
     * 
     * @param v vector 1
     * @param u vector 2
     * @return [v, u] appended result
     */
    public static double[] append(double[] v, double[] u) {
        double[] result = new double[v.length + u.length];
        System.arraycopy(v, 0, result, 0, v.length);
        System.arraycopy(u, 0, result, v.length, u.length);
        return result;
    }

    /**
     * Append the vector u to the vector v and return the result
     * 
     * @param v vector 1
     * @param u vector 2
     * @return [v, u] appended result
     */
    public static int[] append(int[] v, int[] u) {
        int[] result = new int[v.length + u.length];
        System.arraycopy(v, 0, result, 0, v.length);
        System.arraycopy(u, 0, result, v.length, u.length);
        return result;
    }

    /**
     * Copies all rows and columns between two double arrays
     * 
     * @param src
     * @param dest
     */
    public static void arrayCopy(double[][] src, double[][] dest) {
        for (int r = 0; r < src.length; r++) {
            System.arraycopy(src[r], 0, dest[r], 0, src[r].length);
        }
    }

    /**
     * Copies all rows and columns between two double arrays
     * 
     * @param src
     * @param dest
     */
    public static double[][] arrayCopy(double[][] src) {
        double[][] dest = new double[src.length][];
        for (int r = 0; r < src.length; r++) {
            dest[r] = new double[src[r].length];
            System.arraycopy(src[r], 0, dest[r], 0, src[r].length);
        }
        return dest;
    }

    /**
     * Copies the required rows and columns between two 
     * double arrays
     * 
     * @param src
     * @param srcStartRow
     * @param srcStartCol
     * @param dest
     * @param destStartRow
     * @param destStartCol
     * @param rows
     * @param cols
     */
    public static void arrayCopy(double[][] src, int srcStartRow,
            int srcStartCol, double[][] dest, int destStartRow,
            int destStartCol, int rows, int cols) {

        for (int r = 0; r < rows; r++) {
            System.arraycopy(src[srcStartRow + r], srcStartCol,
                    dest[destStartRow + r], destStartCol, cols);
        }
    }

    /**
     * Copies all rows and columns between two int arrays
     * 
     * @param src
     * @param dest
     */
    public static void arrayCopy(int[][] src, int[][] dest) {
        for (int r = 0; r < src.length; r++) {
            System.arraycopy(src[r], 0, dest[r], 0, src[r].length);
        }
    }

    /**
     * Copies the required rows and columns between two 
     * double arrays
     * 
     * @param src
     * @param srcStartRow
     * @param srcStartCol
     * @param dest
     * @param destStartRow
     * @param destStartCol
     * @param rows
     * @param cols
     */
    public static void arrayCopy(int[][] src, int srcStartRow,
            int srcStartCol, int[][] dest, int destStartRow,
            int destStartCol, int rows, int cols) {

        for (int r = 0; r < rows; r++) {
            System.arraycopy(src[srcStartRow + r], srcStartCol,
                    dest[destStartRow + r], destStartCol, cols);
        }
    }
}

Related Tutorials