简体   繁体   English

如何在这个问题中正确地乘以2个矩阵?

[英]How to properly multiply 2 matrices in this problem?

This is my mult method: 这是我的多方法:

public Matrix mult(Matrix otherMatrix) {
if(!colsEqualsOthersRows(otherMatrix)) // checks if Matrix A has the same number of columns as Matrix B has rows 
        return null;
    int multiplication[][] = new int[rows][columns];
    for(int r = 0; r < rows; r++) {
        for(int c = 0; c < otherMatrix.columns; c++) {
            int sum = 0;
            for(int i = 0; i < otherMatrix.columns; i++) {
                sum = sum + matrix[r][i]*otherMatrix.matrix[i][c];
                multiplication[r][c] = sum;
            }
        }
    }
return new Matrix(multiplication);
}

In the driver method, whenever there's a question that involves multiplying matrices it's either wrong or I get an error from the system. 在驱动程序方法中,只要存在涉及乘法矩阵的问题,它就会出错或者我从系统中得到错误。

ie

3BC-4BD //which is

B.mult(3).mult(C)).subtract(B.mult(4).mult(D));

This is the error. 这是错误。

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at lab1.Matrix. mult(Matrix.java:81) at lab1.Driver. main(Driver.java:128) 

These are the matrices I'm using. 这些是我正在使用的矩阵。

Matrix A = new Matrix(new int[][] {{1,-2,3},{1,-1,0}});
    Matrix B = new Matrix(new int[][] {{3,4},{5,-1},{1,-1}});
    Matrix C = new Matrix(new int[][] {{4,-1,2},{-1,5,1}});
    Matrix D = new Matrix(new int[][] {{-1,0,1},{0,2,1}});
    Matrix E = new Matrix(new int[][] {{3,4},{-2,3},{0,1}});
    Matrix F = new Matrix(new int[][] {{2},{-3}});
    Matrix G = new Matrix(new int[][] {{2,-1}});

This is my Matrix class: 这是我的Matrix类:

public class Matrix { 
    int [][] matrix; 
    int rows, columns; 

    public Matrix (int[][] m) { 
        this.matrix = m; 
        this.rows = m.length; 
        this.columns = m[0].length; 
    }
} 

I'm a beginner in the JAVA language so please excuse my ignorance. 我是JAVA语言的初学者,请原谅我的无知。 Please help! 请帮忙!

Notice that output of the matrix multiplication is as follow: A(nXm) * B (mXk) = C (nXk) 请注意,矩阵乘法的输出如下: A(nXm) * B (mXk) = C (nXk)

In your case: B(2X3) * C(3X2) = Output(2X2) 在您的情况下: B(2X3) * C(3X2) = Output(2X2)

However your code define the output matrix with the dimension of the first one (as can be see here: int multiplication[][] = new int[rows][columns]; ) 但是你的代码用第一个维度来定义输出矩阵(可以在这里看到: int multiplication[][] = new int[rows][columns];

In order to fix that try (add 2 small optimization as set the multiplication[r][c] outside the inner loop): 为了解决这个问题(添加2个小优化,设置内部循环外的multiplication[r][c] ):

int multiplication[][] = new int[rows][otherMatrix.columns];
for(int r = 0; r < rows; r++) {
    for(int c = 0; c < otherMatrix.columns; c++) {
        int sum = 0;
        for(int i = 0; i < otherMatrix.columns; i++)
            sum += matrix[r][i]*otherMatrix.matrix[i][c];
        multiplication[r][c] = sum;
    }

First of all the new matrix is this.rows, otherMatrix.columns and when multiplying you are checking twice otherMatrix.columns and I think is the second for the one that should be this.columns 首先,新矩阵是this.rows,otherMatrix.columns,当你相乘时你正在检查两次其他的Matrix.columns,我认为是第二个应该是this.columns

 public Matrix mult(Matrix otherMatrix) {
    if(!colsEqualsOthersRows(otherMatrix)) // checks if Matrix A has the same number of columns as Matrix B has rows
        return null;
    int multiplication[][] = new int[rows][otherMatrix.columns];
    for(int r = 0; r < rows; r++) {
        for(int c = 0; c < otherMatrix.columns; c++) {
            int sum = 0;
            for(int i = 0; i < columns; i++) {
                sum = sum + matrix[r][i]*otherMatrix.matrix[i][c];
                multiplication[r][c] = sum;
            }
        }
    }
    return new Matrix(multiplication);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM