# Problems with 2 dimensional matrix multiplication in java

I have been having problems with one of my java functions that is supposed to 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 i variable is supposed to cycle through each element of m (the first matrix) in the for loop. The j variable is supposed to cycle through each row of the second matrix n and the variable k is supposed to cycle through each element in the first row of the first matrix and the first column of the second matrix. This does not seem to be working correctly and when given the input

```[[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]]
```

it gives out

```[[30.0, 36.0, 0.0],
[78.0, 96.0, 0.0],
[0.0, 0.0, 0.0]]
```

rather than

```[[34.0, 44.0, 54.0],
[86.0, 112.0, 138.0],
[30.0, 45.0, 60.0]].
```

I cannot understand why this is?

Fix:

```     public static double[][] matrixMultiply(double[][] m, double[][] n) {
double[][] multipliedMatrix = new double [m.length][n[0].length];
for (int i=0; i<m.length; i++)
{
for (int j=0; j<n[0].length; j++)
{
for (int k=0; k<n.length; k++)
{
multipliedMatrix[i][j] = multipliedMatrix[i][j] + (m[i][k] * n[k][j]);
}
}
}
return multipliedMatrix;
}
```

OUTPUT

```34.044.054.0
86.0112.0138.0
30.045.060.0
```

Explanation In each loop you should run while the index is smaller than length - not smaller than length-1

It is also possible to save row / column length to separate variables. This will avoid confusion of what row / column of what matrix is used.

Java. Matrix multiplication.

```public class Matrix {

/**
* Matrix multiplication method.
* @param m1 Multiplicand
* @param m2 Multiplier
* @return Product
*/
public static double[][] multiplyByMatrix(double[][] m1, double[][] m2) {
int m1ColLength = m1[0].length; // m1 columns length
int m2RowLength = m2.length;    // m2 rows length
if(m1ColLength != m2RowLength) return null; // matrix multiplication is not possible
int mRRowLength = m1.length;    // m result rows length
int mRColLength = m2[0].length; // m result columns length
double[][] mResult = new double[mRRowLength][mRColLength];
for(int i = 0; i < mRRowLength; i++) {         // rows from m1
for(int j = 0; j < mRColLength; j++) {     // columns from m2
for(int k = 0; k < m1ColLength; k++) { // columns from m1
mResult[i][j] += m1[i][k] * m2[k][j];
}
}
}
return mResult;
}

public static String toString(double[][] m) {
String result = "";
for(int i = 0; i < m.length; i++) {
for(int j = 0; j < m[i].length; j++) {
result += String.format("%11.2f", m[i][j]);
}
result += "\n";
}
return result;
}

public static void main(String[] args) {
// #1
double[][] multiplicand = new double[][] {
{3, -1, 2},
{2,  0, 1},
{1,  2, 1}
};
double[][] multiplier = new double[][] {
{2, -1, 1},
{0, -2, 3},
{3,  0, 1}
};
System.out.println("#1\n" + toString(multiplyByMatrix(multiplicand, multiplier)));
// #2
multiplicand = new double[][] {
{1, 2, 0},
{-1, 3, 1},
{2, -2, 1}
};
multiplier = new double[][] {
{2},
{-1},
{1}
};
System.out.println("#2\n" + toString(multiplyByMatrix(multiplicand, multiplier)));
// #3
multiplicand = new double[][] {
{1, 2, -1},
{0,  1, 0}
};
multiplier = new double[][] {
{1, 1, 0, 0},
{0, 2, 1, 1},
{1, 1, 2, 2}
};
System.out.println("#3\n" + toString(multiplyByMatrix(multiplicand, multiplier)));
}
}
```

Output:

```#1
12.00      -1.00       2.00
7.00      -2.00       3.00
5.00      -5.00       8.00

#2
0.00
-4.00
7.00

#3
0.00       4.00       0.00       0.00
0.00       2.00       1.00       1.00
```