Problems with two-dimensional matrix multiplication in java

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?

+4
source share
2 answers

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


, - -1

+5

/ . , / .

Java. .

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)));
    }
}

:

#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
0

All Articles