public class MatrixMultiply {
public static int[][] matrixMultiply(int a[][], int b[][]) {
int c[][] = new int[a.length][b[0].length];
if (a[0].length == b.length) {
// 控制循环次数
for(int i=0;i<a.length;i++){
for (int j = 0; j < b[0].length; j++) {
int sum = 0;
for (int k = 0; k < a.length; k++) {
sum = sum + a[i][k] * b[k][j];
System.out.println(a[i][k]+" * "+b[k][j]+"----->"+sum);
}
c[i][j]=sum;
}
}
} else {
System.out.println("矩阵不能相乘!");
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = { { 1, 2, 3 },
{ 4, 5, 6 } };
int b[][] = { { 1, 2 },
{ 3, 4 },
{ 5, 6 } };
MatrixMultiply .matrixMultiply(a, b);
}
}
JAVA实现矩阵连乘问题
最新推荐文章于 2021-12-03 11:49:41 发布