输入两个数组,利用二维数组编程实现矩阵相乘、矩阵转置Java源代码
时间: 2023-06-05 08:08:10 浏览: 142
抱歉,我可以回答你提出的问题,以下是实现矩阵相乘、矩阵转置的Java源代码:
// 矩阵相乘
public static int[][] matrixMultiply(int[][] a, int[][] b) {
int m = a.length;
int n = a[0].length;
int p = b[0].length;
int[][] c = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
// 矩阵转置
public static int[][] transpose(int[][] a) {
int m = a.length;
int n = a[0].length;
int[][] b = new int[n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[j][i] = a[i][j];
}
}
return b;
}
阅读全文