gpt4 book ai didi

java - 尝试乘以二维数组(如矩阵乘法),但仅适用于方阵

转载 作者:行者123 更新时间:2023-12-02 05:06:12 29 4
gpt4 key购买 nike

我是一个完全的 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();
}
}
}

最佳答案

我学习线性代数已经有一段时间了,但我认为当你将矩阵 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

关于java - 尝试乘以二维数组(如矩阵乘法),但仅适用于方阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769616/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com