📘 Lesson · Lesson 54
Matrix Multiplication
Matrix Multiplication
Matrix Multiplication Program in Java
Matrix multiplication combines two matrices using three nested loops (row × column sums).
Java Program
int[][] a = {{1,2},{3,4}};
int[][] b = {{5,6},{7,8}};
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
c[i][j] += a[i][k] * b[k][j];
System.out.println(java.util.Arrays.deepToString(c));
Output
[[19, 22], [43, 50]]
How it Works
- Each result cell is the sum of products of a row and a column.
- Three loops: rows, columns, and the shared dimension k.
Summary
- Matrix multiplication combines two matrices using three nested loops (row × column sums).
Matrix Multiplication Program in Java
Matrix multiplication तीन nested loops से दो matrices को जोड़ता है (row × column sums)।
Java Program
int[][] a = {{1,2},{3,4}};
int[][] b = {{5,6},{7,8}};
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
c[i][j] += a[i][k] * b[k][j];
System.out.println(java.util.Arrays.deepToString(c));
Output
[[19, 22], [43, 50]]
कैसे काम करता है
- हर result cell एक row और column के products का योग है।
- तीन loops: rows, columns, और shared dimension k।
सारांश
- Matrix multiplication तीन nested loops से दो matrices को जोड़ता है (row × column sums)।