我是一个完整的Java新手,我试图将两个2D数组相乘,就像你将两个矩阵相乘一样。以下程序仅适用于方形矩阵,但不适用于其他矩阵。我似乎无法弄清楚我哪里出错了。如果有人可以帮助我,那就太好了。
import java.util.Scanner;
public class TwoDMatrix {
public static void main (String [] args){
Scanner scanme = new Scanner(System.in);
//Input dimensions of Matrix A
System.out.println("Enter the dimensions (row x column) of Matrix A");
int rowA = scanme.nextInt();
int columnA = scanme.nextInt();
int [][] matA = new int [rowA][columnA];
//Input dimensions of Matrix B
System.out.println("Enter the dimensions (row x column) of Matrix B");
int rowB = scanme.nextInt();
int columnB = scanme.nextInt();
int [][] matB = new int [rowB][columnB];
// Declaring new variables
int [][] product = new int [columnA][rowB];
int rowCountA, columnCountA, rowCountB, columnCountB;
int rowCountProduct, columnCountProduct;
int sum;
String divider = "---------";
// Input values of Matrix A
for (rowCountA = 0; rowCountA < rowA; rowCountA++){
for (columnCountA = 0; columnCountA < columnA; columnCountA++){
System.out.printf("%s%d%s%d%s", "Enter the value at A(", rowCountA, ",", columnCountA, ")");
matA[rowCountA][columnCountA] = scanme.nextInt();
}
}
// Input values of Matrix B
for (rowCountB = 0; rowCountB < rowB; rowCountB++){
for (columnCountB = 0; columnCountB < columnB; columnCountB++){
System.out.printf("%s%d%s%d%s", "Enter the value at B(", rowCountB, ",", columnCountB, ")");
matB[rowCountB][columnCountB] = scanme.nextInt();
}
}
//Calculate product of the two matrices
for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){
for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){
sum = 0;
for (columnCountA=0, rowCountB=0; columnCountA<columnA && rowCountB<rowB; columnCountA++, rowCountB++){
sum += (matA[rowCountProduct][columnCountA] * matB[rowCountB][columnCountProduct]);
}
product[rowCountProduct][columnCountProduct] = sum;
}
}
//Prints the input matrix A
System.out.printf("%n%s%n%s%n", "Matrix A:", divider);
for (rowCountA = 0; rowCountA < rowA; rowCountA++){
for (columnCountA = 0; columnCountA < columnA; columnCountA++){
System.out.printf("%5d", matA[rowCountA][columnCountA]);
}
System.out.println();
}
//Prints the input matrix B
System.out.printf("%n%s%n%s%n", "Matrix B:", divider);
for (rowCountB = 0; rowCountB< rowB; rowCountB++){
for (columnCountB = 0; columnCountB < columnB; columnCountB++){
System.out.printf("%5d", matB[rowCountB][columnCountB]);
}
System.out.println();
}
//Prints the product
System.out.printf("%n%s%n%s%n", "Product", divider);
for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){
for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){
System.out.printf("%5d", product[rowCountProduct][columnCountProduct]);
}
System.out.println();
}
}
}
答案 0 :(得分:1)
我学习线性代数已经有一段时间了,但我认为当你将矩阵A [n1] [m1]乘以矩阵B [n2] [m2]时,m1必须等于n2,结果应该是是一个矩阵C [n1] [m2]。
因此
int [][] product = new int [columnA][rowB];
应该是
int [][] product = new int [rowA][columnB];
在开始乘法之前,您应该验证columnA == rowB
。
答案 1 :(得分:1)
你的情况非常复杂:
for (columnCountA=0, rowCountB=0; columnCountA<columnA && rowCountB<rowB; columnCountA++, rowCountB++){
sum += (matA[rowCountProduct][columnCountA] * matB[rowCountB][columnCountProduct]);
}
记住数学公式:
be A a n x l matrix, B a l x m matrix, then
forall (i,j) in [1,n]x[1,m], (AB)(i,j) = sum_(k in [1,l]) { A(i,k).B(k,j) }
因此,伪代码是:
for (int i=0 ; i<A.length ; i++) {
for (int j=0 ; j<B[0].length ; j++) {
prod[i][j] = 0;
for (int k=0 ; k<A[0].length ; k++) {
prod[i][j] += A[i][k]*B[k][j];
}
}
}
答案 2 :(得分:0)
基本上,您将产品矩阵定义为:
int [][] product = new int [columnA][rowB];
这意味着它应该包含与A中的列一样多的行,以及与B中的行一样多的列。
但是,当你循环填充它时,这就是你的循环:
for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){
for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){
...
}
}
这意味着您正在尝试填充产品中的行,这些行应该在0
≤rowCountProduct
&lt;范围内。 columnA
的值范围为0
≤rowCountProduct
&lt; rowA
columnB
。同样,您可以按照您的定义将列运行到范围rowB
而不是{{1}}。
因此,您应该更改矩阵的定义,或者更改填充矩阵的方式。