4

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?

4
  • I did not explain myself very well initially and I find it hard to put into words but that is exactly what the code is doing yet not yeilding correct results Commented Nov 16, 2013 at 23:32
  • 1
    @Chandranshu he actually understands pretty well how to preform matrix multiplication. If you would follow his code you wouldn't post that comment. Commented Nov 16, 2013 at 23:35
  • Yes, I read the code after his comment. Could you please update the question statement? I'll just delete the comment. Commented Nov 16, 2013 at 23:38
  • 1
    @Chandranshu his question is very clear, only relevant code was posted along with the input and output vs. expected output. I wish all people would post their questions this way... Commented Nov 16, 2013 at 23:39

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!!!!!! 2 hours pondering over the code and I did not even consider the loop length
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.