java编程实现将1个4行3列的矩阵转置;
时间: 2023-05-19 17:05:03 浏览: 78
java实现矩阵转置
5星 · 资源好评率100%
可以使用以下代码实现:
```java
public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
int[][] transposedMatrix = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
transposedMatrix[j][i] = matrix[i][j];
}
}
for (int i = 0; i < transposedMatrix.length; i++) {
for (int j = 0; j < transposedMatrix[0].length; j++) {
System.out.print(transposedMatrix[i][j] + " ");
}
System.out.println();
}
}
}
```
这个程序会将一个4行3列的矩阵转置,输出结果为:
```
1 4 7 10
2 5 8 11
3 6 9 12
```
阅读全文