Hello. It is necessary to implement matrix multiplication, here is the code:

public class MatrixMultiplier { private int[][] transportMatrix(int[][] a) { int[][] b = new int[a.length][a.length]; for (int i = 0; i < a.length; ++i) for (int j = 0; j < a.length; ++j) { b[i][j] = a[j][i]; } return b; } public int[][] multiply(int[][] mA, int[][] mB) { int mSize = mA.length; mB = transportMatrix(mB); int nSize = mB[0].length; int o = mB.length; int[][] mResult = new int[mSize][nSize]; for (int i = 0; i < mSize; i++) { for (int j = 0; j < nSize; j++) { for (int k = 0; k < o; k++) { System.out.print(i + " " + j); mResult[i][j] += mA[i][k] * mB[k][j]; } } } for (int i = 0; i < mResult.length; i++) { for (int j = 0; j < mResult[0].length; j++) { System.out.format("%6d ", mResult[i][j]); } System.out.println(); } return mResult; } } public class MatrixMultiply { private static int[][] mA; private static int[][] mB; private static int[][] mResult; public static void main(String[] args) { MatrixMultiplier matrixMultiplier = new MatrixMultiplier(); mA = new int[][] {{5,3,2}, {3,1,0}, {1,4,1}, {8,2,1}}; mB = new int[][] {{5,6,2}, {3,1,0}, {1,4,1}, {8,2,1}, {3,1,1}}; mResult = new int[mA.length][mB[0].length]; mResult = matrixMultiplier.multiply(mA, mB); } } 

The result is an exception:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at matrix.MatrixMultiplier.transportMatrix(MatrixMultiplier.java:8) at matrix.MatrixMultiplier.multiply(MatrixMultiplier.java:15) at matrix.MatrixMultiply.main(MatrixMultiply.java:23) 

Why?

  • one
    because you have both cycles in the same dimension (number of rows), and the matrices are not square. - zRrr
  • @Schepalin please answer the answer if helped. - Vladislav Kuznetsov

1 answer 1

Of the errors there were problems in the function for transposing the matrix and in the cycles for multiplication, you are confused as to which indices of the elements should be set. Here, compare with yours, I removed the errors:

  public class MatrixMultiply { private static int[][] mA, mB, mResult; public static void main(String[] args) { MatrixMultiplier matrixMultiplier = new MatrixMultiplier(); mA = new int[][]{{5, 3, 2}, {3, 1, 0}, {1, 4, 1}, {8, 2, 1}}; mB = new int[][]{{5, 6, 2}, {3, 1, 0}, {1, 4, 1}, {8, 2, 1}, {3, 1, 1}}; mResult = new int[mA.length][mB.length]; System.out.println("Первая матрица"); matrixMultiplier.print(mA); System.out.println("Вторая матрица"); mB = matrixMultiplier.transportMatrix(mB); matrixMultiplier.print(mB); System.out.println("Результирующая матрица"); mResult = matrixMultiplier.multiply(mA, mB); matrixMultiplier.print(mResult); } } class MatrixMultiplier{ public int[][] transportMatrix(int[][] a) { int[][] b = new int[a[0].length][a.length]; for (int i = 0; i < a[0].length; i++) for (int j = 0; j < a.length; j++) { b[i][j] = a[j][i]; } return b; } public int[][] multiply(int[][] mA, int[][] mB) { int mSize = mA.length; int nSize = mB[0].length; int o = mB.length; int[][] mResult = new int[mSize][nSize]; for (int i = 0; i < mSize; i++) { for (int j = 0; j < nSize; j++) { for (int k = 0; k < o; k++) { mResult[i][j] += mA[i][k] * mB[k][j]; } } } return mResult; } public void print(int[][] m) { for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[0].length; j++) { System.out.format("%6d ", m[i][j]); } System.out.println(); } } } 
  • Updated the answer, study. - Vladislav Kuznetsov
  • Yes thank you. Errors, in principle, was not - the fact is that I forgot that matrix multiplication is feasible only when the width of the first matrix = the height of the second - lounah