Matrix is one of the fundamental concepts in java. A matrix is a structure which consists of rows and columns.
Logic behind matrix multiplication:
- When you want to multiply two matrices, there are some points to be noted.
- First, columns of first matrix and rows of second matrix should be equal.
- Iterate through each row of matrix1 and each column of matrix2 and add the elements.
- Final result matrix rows should be equal to matrix1. The resultant matrix column should be equal to matrix2.
Program implementation:
- It starts with including the built in package java.util.Scanner.
- A public class is created with main() function.
- A scanner object gets assigned with System input.
- The dimensions of two matrices (rows and columns) received from the user. The columns of first matrix are equal to the rows of second matrix.
- The values for first and second matrices are get assigned from user input.
- Using three for loops, the rows are iterated with columns and added its value.
- Finally, it gets assigned to result_matrix.
- At last, the result_matrix is displayed in the screen.
Program:
import java.util.Scanner;
public class MMultiplicationExample {
public static void
main(String[] args) {
Scanner s1 =
new Scanner(System.in);
// Get the
dimensions of matrix
System.out.print("Enter rows for first matrix: ");
int r1 =
s1.nextInt();
System.out.print("Enter columns for first matrix / rows for second
matrix: ");
int cR =
s1.nextInt();
System.out.print("Enter columns for second matrix: ");
int c2 =
s1.nextInt();
// Let us
Initialize matrices
int[][]
matrix_1 = new int[r1][cR];
int[][]
matrix_2 = new int[cR][c2];
int[][]
result_matrix = new int[r1][c2];
// read the
input for Matrix 1
System.out.println("Enter the elements for the first
matrix:");
for (int i =
0; i < r1; i++) {
for (int j
= 0; j < cR; j++) {
matrix_1[i][j] = s1.nextInt();
}
}
//read the
input for Matrix 2
System.out.println("Enter the elements for the second
matrix:");
for (int i =
0; i < cR; i++) {
for (int j
= 0; j < c2; j++) {
matrix_2[i][j] = s1.nextInt();
}
}
// Multiply
the matrices
for (int i =
0; i < r1; i++) {
for (int j
= 0; j < c2; j++) {
result_matrix[i][j] = 0;
for
(int k = 0; k < cR; k++) {
result_matrix[i][j] += matrix_1[i][k] * matrix_2[k][j];
}
}
}
// Display the
Resultant matrix
System.out.println("Here is the Resultant matrix:");
for (int i =
0; i < r1; i++) {
for (int j
= 0; j < c2; j++) {
System.out.print(result_matrix[i][j] + " ");
}
System.out.println();
}
s1.close();
}
}
Output:
C:\raji\blog>javac MMultiplicationExample.java
C:\raji\blog>java MMultiplicationExample
Enter rows for first matrix: 3
Enter columns for first matrix / rows for second matrix: 2
Enter columns for second matrix: 2
Enter the elements for the first matrix:
1
2
3
4
5
6
Enter the elements for the second matrix:
3
6
9
2
Here is the Resultant matrix:
21 10
45 26
69 42
That’s all. The java implementation of Matrix Multiplication
is done. Hope, you are interested in this code. Keep coding.
No comments:
Post a Comment