I am trying to make the product of two int[][] (matrices).
If I have the two matrices a and b I would like to calculate matrix c which
is the product of a and b:
In this example I use two matrices with length 2 that contains the same
numbers.
a: 1 2 b:1 2 = c: 7 10
3 4 3 4 15 22
Here I create matrix a and matrix b:
int[][] a = new int[2][2];
int[][] b = new int[2][2];
int fill = 1;
for (int i = 0; i < 2; i++){
for (int k = 0; k < 2; k++){
a[i][k] = fill;
b[i][k] = fill;
fill++;
}
}
Here I try yo make the calculation:
int result = 0;
int result2 = 0;
int[]store = new int[4]; // Where I store the values of the product
matrix x.
for (int m = 0; m < 2; m++){
for (int n = 0; n < 2; n++){
result = result + (a[m][n] * b[n][m]);
}
result2 = result;
store[m*m] = result2; // 7 and 22 get stored in my array.
result = 0;
}
}
}
But I can only seem to calculate "7" and "22"! Does anybody have a hint for
how to get the other two result of c calculated??
John - 28 Feb 2005 14:02 GMT
> I am trying to make the product of two int[][] (matrices).
>
[quoted text clipped - 40 lines]
> But I can only seem to calculate "7" and "22"! Does anybody have a hint for
> how to get the other two result of c calculated??
There is no need to reinvent the wheel.
http://math.nist.gov/javanumerics/jama/
John
Chris Smith - 28 Feb 2005 14:16 GMT
> Here I try yo make the calculation:
>
[quoted text clipped - 16 lines]
> But I can only seem to calculate "7" and "22"! Does anybody have a hint for
> how to get the other two result of c calculated??
If you're looking for the matrix cross product, then you need to
reconsider the logic a bit. Essentially, when computing a cell of the
result, there is a row and a column, and then a third number that stands
in for row/column of the other matrix. That's three nested loops.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation