'is there any way to simplify my code for matrix multiplication?

public class J1_LAB08_1 {
public static void main(String[] args) {
    int r1 = 5, c1 = 3;//rows and column for matrix a
    int r2 = 3, c2 = 4;//rows and column for matrix b
    int matrixA[][] = new int[5][3];//initialize matrix a
    System.out.println("Matrix A");
    for (int i = 0; i < matrixA.length; i++) {
        for (int j = 0; j < matrixA[i].length; j++) {//create random single integer matrix
            matrixA[i][j] = ((int) (Math.random() * 10));
            System.out.print(matrixA[i][j]);//print matrix a
        }
        System.out.println();
    }
    System.out.println();
    System.out.println("Matrix B");
    int matrixB[][] = new int[3][4];
    for (int i = 0; i < matrixB.length; i++) {
        for (int j = 0; j < matrixB[i].length; j++) {//create random single integer matrix
            matrixB[i][j] = ((int) (Math.random() * 10));
            System.out.print(matrixB[i][j]);//print matrix b
        }
        System.out.println();
    }
    System.out.println();
    int[][] matrixC = multiplyMatrices(matrixA, matrixB, r1, c1, c2);//pass variables to multiply matrices function
    printC(matrixC);//print matrix c

}
public static int[][] multiplyMatrices(int[][] matrixA, int[][] matrixB, int r1, int c1, int c2){//multiply the 2 given matrices and return the product
    int[][] matrixC = new int[r1][c2];//initialize the product matrix using row a and column 2 as its parameters
    for(int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            for (int k = 0; k < c1; k++) {
                matrixC[i][j] += matrixA[i][k] * matrixB[k][j];//multiply using textbook formula
            }
        }
    }
    return matrixC;
}
public static void printC(int[][] matrixC) {//print the matrix
    System.out.println("A x B is: ");
    for(int[] row : matrixC) {
        for (int column : row) {
            System.out.print(column + "    ");
        }
        System.out.println();
    }
}

}

is there any way to simplify my code for matrix multiplication? the resulting matrix should be 3 x 5 after multiplication any help would be greatly appreciated, thank you for your time and consideration.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source