public class MatrixMultiply {

    public static void main(String[] args) {
        int[][] matrix1 = new int[][]{{1,3}, {2, 4}};
        int[][] matrix2 = new int[][]{{1,3, 4, 5}, {2, 4, 7, 9}};

        int[][] ans = mutipleMatrix(matrix1, matrix2);

        for (int i = 0; i < ans.length; i++) {
            System.out.println(Arrays.toString(ans[i]).toString());
        }
    }

    public static int[][] mutipleMatrix(int[][] matrix1, int[][] matrix2) {
        int row1 = matrix1.length;
        int col1 = matrix1[0].length;

        int col2 = matrix2[0].length;

        int[][] ans = new int[row1][col2];

        for(int i = 0; i < row1; i++) {

            // row matrix[i] multiply column matrix2[*][j]
            for(int k = 0; k < col2; k++) {
                for (int j = 0; j < col1; j++) {
                    ans[i][k] += matrix1[i][j] * matrix2[j][k];
                }
            }
        }

        return ans;
    }
}
Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-12-04 12:17:49

results matching ""

    No results matching ""