Matrix Multiplication In Java – Here, we will discuss the various methods on how to multiply two matrices using Java. The compiler has been added so that you can execute the given programs yourself, alongside suitable examples and sample outputs.
- Using For Loop
- Using While Loop
- Using Do-While Loop
How To Perform Matrix Multiplication:
Matrix multiplication is a simple binary operation that produces a single matrix from the entries of two given matrices. When two Matrices P & Q of order a*b and b*c are multiplied, the resultant matrix will be of the order a*c.
Here, the a entries across a row of P are multiplied with the b entries down a column of Q to produce the entry of PQ. The example given below explains the multiplication between two 2*2 matrices.
Matrix Multiplication In Java – Using For Loop
1) Condition for multiplication of two matrices is -1st matrix column number equal to 2nd matrix row number.
2) Read row,column numbers of matrix1, matrix2 and check column number of matrix1= row number of matrix2. If condition is true then
a) Insert the elements at matrix1 using two for loops:
- for ( i= 0 ; i < r1 ; i++ )
- for ( j= 0 ; j < c1 ;j++ )
- Insert element at mat1[i][j].
b) Similarly insert the elements at matrix2 using 2 loops with the structures for (i=0;i<r2;i++ ),for (j= 0;j<c2;j++ ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import java.util.Scanner; class MUlMatrix { public static void main(String args[]) { int r1, r2,c1,c2,i,j,k,sum; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows of matrix1"); r1 = in.nextInt(); System.out.println("Enter the number columns of matrix 1"); c1 = in.nextInt(); System.out.println("Enter the number of rows of matrix2"); r2 = in.nextInt(); System.out.println("Enter the number of columns of matrix 2"); c2 = in.nextInt(); if(c1==r2) { int mat1[][] = new int[r1][c1]; int mat2[][] = new int[r2][c2]; int res[][] = new int[r1][c2]; System.out.println("Enter the elements of matrix1"); for ( i= 0 ; i < r1 ; i++ ) { for ( j= 0 ; j < c1 ;j++ ) mat1[i][j] = in.nextInt(); } System.out.println("Enter the elements of matrix2"); for ( i= 0 ; i < r2 ; i++ ) { for ( j= 0 ; j < c2 ;j++ ) mat2[i][j] = in.nextInt(); } System.out.println("\n\noutput matrix:-"); for ( i= 0 ; i < r1 ; i++ ) for ( j= 0 ; j <c2;j++) { sum=0; for ( k= 0 ; k <r2;k++ ) { sum +=mat1[i][k]*mat2[k][j] ; } res[i][j]=sum; } for ( i= 0 ; i < r1; i++ ) { for ( j=0 ; j < c2;j++ ) System.out.print(res[i][j]+" "); System.out.println(); } } else System.out.print("multipication does not exist "); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Enter the number of rows of matrix1 3 Enter the number columns of matrix 1 3 Enter the number of rows of matrix2 3 Enter the number of columns of matrix 2 3 Enter the elements of matrix1 1 2 3 4 5 6 7 8 9 Enter the elements of matrix2 9 8 7 6 5 4 3 2 1 output matrix:- 30 24 18 84 69 54 138 114 90 |
Using While Loop
1) Read row, column numbers of the two matrices and checks the column number of matrix1 =row number of matrix2.If condition true then insert the elements into the matrices using while loop.
2) a) Insert elements at matrix1-
- While loop iterates until i <r1 condition is false.
- then j initialized to 0.
- while loop iterates until the condition j<c1 is true.
- insert the element at mat1[i][j].
- increase the j value.
- Now i value increased.
b) Similarly insert elements at matrix2 with the conditions i<r2,j<c2.
2) For multiplication
- while loop iterates until i<r1 is false.
- j initialized to 0.
- while loop iterates until j<c2 is false.
- sum initialized to 0.
- k=0.
- while loop iterates until k<r2.
- sum =sum+mat1[i][k]*mat2[k][j].
- increases the k value.
- insert sum value in to the resultant matrix at res[i][j].
- increase the j value.
- increase i value.
3) Print the matrix using while loops with the conditions i<r1, j<c2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import java.util.Scanner; class MUlMatrix { public static void main(String args[]) { int r1, r2,c1,c2,i,j,k,sum; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows of matrix1"); r1 = in.nextInt(); System.out.println("Enter the number columns of matrix 1"); c1 = in.nextInt(); System.out.println("Enter the number of rows of matrix2"); r2 = in.nextInt(); System.out.println("Enter the number of columns of matrix 2"); c2 = in.nextInt(); if(c1==r2) { int mat1[][] = new int[r1][c1]; int mat2[][] = new int[r2][c2]; int res[][] = new int[r1][c2]; System.out.println("Enter the elements of matrix1"); i= 0 ; while( i < r1 ) { j= 0 ; while( j < c1) { mat1[i][j] = in.nextInt(); j++ ; } i++; } System.out.println("Enter the elements of matrix2"); i= 0 ; while( i < r2 ) { j= 0 ; while( j < c2) { mat2[i][j] = in.nextInt(); j++ ; } i++; } System.out.println("\n\nOriginal matrix:-"); i= 0 ; while( i < r1 ) { j= 0 ; while( j <c2) { sum=0; k= 0 ; while(k <r2) { sum +=mat1[i][k]*mat2[k][j] ; k++ ; } res[i][j]=sum; j++; } i++; } i= 0 ; while( i < r1 ) { j=0 ; while( j < c2 ) { System.out.print(res[i][j]+" "); j++; } System.out.println(); i++ ; } } else System.out.print("multipication does not exist "); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Enter the number of rows 2 Enter the number columns 3 Enter the elements of matrix 1 2 3 4 5 6 Original matrix:- 1 2 3 4 5 6 Transpose of matrix:- 1 4 2 5 3 6 |
Using Do While Loop
1) Multiply two matrices mat1,mat2, res is the resultant matrix.
- This loop iterates until k<r2 is false.
- Insert sum in to the resultant matrix at res[i][j],increase j value, then checks the condition j<c2.
- This loop iterates until j<c2 is false.
- Increase the i value, then checks the condition i<r1.
- This loop iterates until i<r1 is false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import java.util.Scanner; class MUlMatrix { public static void main(String args[]) { int r1, r2,c1,c2,i,j,k,sum; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows of matrix1"); r1 = in.nextInt(); System.out.println("Enter the number columns of matrix 1"); c1 = in.nextInt(); System.out.println("Enter the number of rows of matrix2"); r2 = in.nextInt(); System.out.println("Enter the number of columns of matrix 2"); c2 = in.nextInt(); if(c1==r2) { int mat1[][] = new int[r1][c1]; int mat2[][] = new int[r2][c2]; int res[][] = new int[r1][c2]; System.out.println("Enter the elements of matrix1"); i= 0 ; do { j= 0 ; do { mat1[i][j] = in.nextInt(); j++ ; } while( j < c1); i++; }while( i < r1 ); System.out.println("Enter the elements of matrix2"); i= 0 ; do { j= 0 ; do { mat2[i][j] = in.nextInt(); j++ ; }while( j < c2); i++; }while( i < r2 ); System.out.println("\n\nOriginal matrix:-"); i= 0 ; do { j= 0 ; do { sum=0; k= 0 ; do { sum +=mat1[i][k]*mat2[k][j] ; k++ ; }while(k <r2); res[i][j]=sum; j++; }while( j <c2); i++; }while( i < r1 ); i= 0 ; do { j=0 ; do { System.out.print(res[i][j]+" "); j++; }while( j < c2 ); System.out.println(); i++ ; }while( i < r1 ); } else System.out.print("multipication does not exist "); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Enter the number of rows of matrix1 2 Enter the number columns of matrix 1 3 Enter the number of rows of matrix2 3 Enter the number of columns of matrix 2 2 Enter the elements of matrix1 1 1 1 1 1 1 Enter the elements of matrix2 1 1 1 1 1 1 Original matrix:- 3 3 3 3 |