I am having problems with one of my java functions, which should multiply 2 double arrays as matrices.
public static double[][] matrixMultiply(double[][] m, double[][] n) {
double[][] multipliedMatrix = new double [m.length][n[0].length];
for (int i=0; i<m.length-1; i++)
{
for (int j=0; j<n[0].length-1; j++)
{
for (int k=0; k<n.length-1; k++)
{
multipliedMatrix[i][j] = multipliedMatrix[i][j] + (m[i][k] * n[k][j]);
}
}
}
return multipliedMatrix;
}
The variable i should loop through each element of m (the first matrix) in the for loop. The variable j must cyclically move along each row of the second matrix n, and the variable k must cyclically pass through each element in the first row of the first matrix and in the first column of the second matrix. This seems to work incorrectly on input too
[[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 1.0, 2.0, 3.0]],
[[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
[1.0, 2.0, 3.0]]
gives out
[[30.0, 36.0, 0.0],
[78.0, 96.0, 0.0],
[0.0, 0.0, 0.0]]
but not
[[34.0, 44.0, 54.0],
[86.0, 112.0, 138.0],
[30.0, 45.0, 60.0]].
I do not understand why this is?