输入两个数组,利用二维数组编程实现矩阵相乘、矩阵转置Java源代码
时间: 2023-06-05 17:08:10 浏览: 79
抱歉,我可以回答你提出的问题,以下是实现矩阵相乘、矩阵转置的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;
}
相关问题
输入两个矩阵,利用二维数组编程实现矩阵相乘、矩阵转置Java源代码
这是 Java 源代码实现矩阵相乘和矩阵转置的示例:
// 矩阵相乘函数
public static double[][] matrixMultiplication(double[][] a, double[][] b) {
int a_rows = a.length;
int a_cols = a[0].length;
int b_cols = b[0].length;
double[][] c = new double[a_rows][b_cols];
for (int i = 0; i < a_rows; i++) {
for (int j = 0; j < b_cols; j++) {
for (int k = 0; k < a_cols; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
// 矩阵转置函数
public static double[][] matrixTranspose(double[][] a) {
int rows = a.length;
int cols = a[0].length;
double[][] b = new double[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[j][i] = a[i][j];
}
}
return b;
}
// 调用示例
public static void main(String[] args) {
double[][] a = {{1, 2, 3}, {4, 5, 6}};
double[][] b = {{7, 8}, {9, 10}, {11, 12}};
double[][] c = matrixMultiplication(a, b);
double[][] d = matrixTranspose(c);
System.out.println(Arrays.deepToString(c));
System.out.println(Arrays.deepToString(d));
}
.利用二维数组编程实现矩阵相乘、矩阵转置Java源代码
好的,请稍等...
以下是使用 Java 编写的二维数组实现矩阵相乘与矩阵转置的源代码:
矩阵相乘:
```
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrixA = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] matrixB = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int[][] result = multiply(matrixA, matrixB);
printMatrix(result);
}
public static int[][] multiply(int[][] matrixA, int[][] matrixB) {
int aRows = matrixA.length;
int aCols = matrixA[0].length;
int bRows = matrixB.length;
int bCols = matrixB[0].length;
if (aCols != bRows) {
throw new IllegalArgumentException("The number of columns in matrix A must equal the number of rows in matrix B!");
}
int[][] result = new int[aRows][bCols];
for (int i = 0; i < aRows; i++) {
for (int j = 0; j < bCols; j++) {
for (int k = 0; k < aCols; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
```
矩阵转置:
```
public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] result = transpose(matrix);
printMatrix(result);
}
public static int[][] transpose(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] result = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
return result;
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
```
希望能帮到你!