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

Java 在这个问题中如何正确地乘以2个矩阵?,java,arrays,matrix,Java,Arrays,Matrix,这是我的mult方法: 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(i

这是我的mult方法:

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);
}
这就是错误所在

这些是我正在使用的矩阵

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}});
这是我的矩阵课程:

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

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

我是JAVA语言的初学者,请原谅我的无知。请帮忙

请注意,矩阵乘法的输出如下:
A(nXm)*B(mXk)=C(nXk)

在您的情况下:
B(2X3)*C(3X2)=输出(2X2)

但是,您的代码使用第一个矩阵的维度定义输出矩阵(如图所示:
int乘法[][]=newint[rows][columns];

为了解决这个问题,请尝试(在内部循环外部添加两个小优化设置
乘法[r][c]
):

int乘法[][]=newint[行][otherMatrix.columns];
对于(int r=0;r
首先,新矩阵是this.rows,otherMatrix.columns 当相乘时,你要检查两次otherMatrix.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);
}
公共矩阵mult(矩阵其他矩阵){
if(!colsEqualsOthersRows(otherMatrix))//检查矩阵A的列数是否与矩阵B的行数相同
返回null;
整数乘法[][]=新整数[行][其他矩阵.列];
对于(int r=0;r
当您将不同大小的矩阵相乘时会出现问题,如果您从矩阵B中取值,则必须检查B的大小,而不是C的大小。如何在mult方法中解决此问题?共享矩阵代码最内层循环的上限应为列数,而不是其他矩阵列数。此外,请当然,得到的和是在最里面的循环之后,而不是在循环内。@ LoStudio:请更新你的问题。通过一些矩阵乘法的例子来检查你的代码。包括测试,以确保矩阵操作数实际上是兼容的WRT乘法。
public class Matrix { 
    int [][] matrix; 
    int rows, columns; 

    public Matrix (int[][] m) { 
        this.matrix = m; 
        this.rows = m.length; 
        this.columns = m[0].length; 
    }
} 
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;
    }
 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);
}