1public class Matrix {
2 private double[][] multiply(double[][] matrixOne, double[][] matrixTwo) {
3 assert matrixOne[0].length == matrixTwo.length: "width of matrix one must be equal to height of matrix two";
4 double[][] product = new double[matrixOne.length][matrixTwo[0].length];
5 //fills output matrix with 0's
6 for(short l = 0; l<matrixOne.length; l++) {
7 for(short m = 0; m<matrixTwo[0].length; m++) {
8 product[l][m] = 0;
9 }
10 }
11 //takes the dot product of the rows and columns and adds them to output matrix
12 for(short i = 0; i<matrixOne.length; i++) {
13 for(short j = 0; j<matrixTwo[0].length; j++) {
14 for(short k = 0; k<matrixOne[0].length; k++) {
15 product[i][j] += matrixOne[i][k] * matrixTwo[k][j];
16 }
17 }
18 }
19 return product;
20 }
21}
1// matrix multiplication java
2public class MatrixMultiplicationJavaDemo
3{
4 public static int[][] multiplyMatrix(int[][] matrix1, int[][] matrix2, int row, int column, int col)
5 {
6 int[][] multiply = new int[row][col];
7 for(int a = 0; a < row; a++)
8 {
9 for(int b = 0; b < col; b++)
10 {
11 for(int k = 0; k < column; k++)
12 {
13 multiply[a][b] += matrix1[a][k] * matrix2[k][b];
14 }
15 }
16 }
17 return multiply;
18 }
19 public static void printMatrix(int[][] multiply)
20 {
21 System.out.println("Multiplication of two matrices: ");
22 for(int[] row : multiply)
23 {
24 for(int column : row)
25 {
26 System.out.print(column + " ");
27 }
28 System.out.println();
29 }
30 }
31 public static void main(String[] args)
32 {
33 int row = 2, col = 3;
34 int column = 2;
35 int[][] matrixOne = {{1, 2, 3}, {4, 5, 6}};
36 int[][] matrixTwo = {{7, 8}, {9, 1}, {2, 3}};
37 int[][] product = multiplyMatrix(matrixOne, matrixTwo, row, col, column);
38 printMatrix(product);
39 }
40}