Matrix production | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Matrix production

I want to know how this code work not the output ​int r1 = 3, c1 = 2; int r2 = 2, c2 = 3; int[][] A ={ {1, 4}, {2, 5}, {3, 6} } ; int[][] B = { {8, 7, 6}, {5, 4, 3} }; int[][] C = new int[r1][c2]; ​try { ​for(int i = 0; i < r1; i++) { ​ for (int j = 0; j < c2; j++) { ​ for (int k = 0; k < c1; k++) { ​ C[i][j] += A[i][k] * B[k][j]; Is it mean when we found the multiple we summation with old value of array c?

3rd Mar 2019, 5:10 AM
Razan
Razan  - avatar
1 Answer
+ 4
Yes, that is what is being done here. This is simply how matrix multiplication is defined. Any element [i,j] in the final matrix is the sum of the products of the individual elements retrieved from the ith row of the first matrix and jth row of the second matrix (1st element of ith row * 1st element of jth column + 2nd element of ith row * 2nd element of jth column and so on till we exhaust the entire row and column). Check this out for a pictorial representation: https://www.mathsisfun.com/algebra/matrix-multiplying.html
3rd Mar 2019, 5:59 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar