Java 如何交换矩阵中按右对角线对称的元素
我有一个代码可以创建一个二维数组,它可以正常工作,但是现在我想让第一行变成最后一行 就像你有一个专栏Java 如何交换矩阵中按右对角线对称的元素,java,for-loop,multidimensional-array,swap,Java,For Loop,Multidimensional Array,Swap,我有一个代码可以创建一个二维数组,它可以正常工作,但是现在我想让第一行变成最后一行 就像你有一个专栏 1 2 3 4 必须以如下方式加入新阵列: 4 3 2 1 16 12 8 4 15 11 7 3 14 10 6 2 13 9 5 1 public class MatrixTranspose { static int m1[][] = new int[][]{{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12,
1
2
3
4
必须以如下方式加入新阵列:
4 3 2 1
16 12 8 4
15 11 7 3
14 10 6 2
13 9 5 1
public class MatrixTranspose {
static int m1[][] = new int[][]{{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}, {13, 14, 15, 16, 17}};
public static String toString(int[][] m) {
StringBuilder text = new StringBuilder();
for (int row = 0; row < m.length; row++) {
int r[] = m[row];
for (int col = 0; col < r.length; col++) {
if (col > 0) text.append(", ");
text.append(r[col]);
}
text.append("\n");
}
return text.toString();
}
public static int[][] transpose(int[][] m) {
int rows = m.length;
int cols = m[0].length;
int t[][] = new int[cols][]; // first create the empty transpose matrix
for (int trow = 0; trow < cols; trow++) {
t[trow] = new int[rows];
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int tcol = rows-row-1; // transposed tcol is inverted row
int trow = cols-col-1; // transposed trow is inverted col
t[trow][tcol] = m[row][col];
}
}
return t;
}
public static void main(String...params) {
System.out.println(toString(m1));
System.out.println("--");
System.out.println(toString(transpose(m1)));
}
}
我得到的矩阵是:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
新矩阵必须如下所示:
4 3 2 1
16 12 8 4
15 11 7 3
14 10 6 2
13 9 5 1
public class MatrixTranspose {
static int m1[][] = new int[][]{{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}, {13, 14, 15, 16, 17}};
public static String toString(int[][] m) {
StringBuilder text = new StringBuilder();
for (int row = 0; row < m.length; row++) {
int r[] = m[row];
for (int col = 0; col < r.length; col++) {
if (col > 0) text.append(", ");
text.append(r[col]);
}
text.append("\n");
}
return text.toString();
}
public static int[][] transpose(int[][] m) {
int rows = m.length;
int cols = m[0].length;
int t[][] = new int[cols][]; // first create the empty transpose matrix
for (int trow = 0; trow < cols; trow++) {
t[trow] = new int[rows];
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int tcol = rows-row-1; // transposed tcol is inverted row
int trow = cols-col-1; // transposed trow is inverted col
t[trow][tcol] = m[row][col];
}
}
return t;
}
public static void main(String...params) {
System.out.println(toString(m1));
System.out.println("--");
System.out.println(toString(transpose(m1)));
}
}
这是我的代码:
package test5;
public class test5 {
public static void main(String[] args) {
int[][] nums = new int[4][4];
nums[0][0] = 1;
nums[0][1] = 2;
nums[0][2] = 3;
nums[0][3] = 4;
nums[1][0] = 5;
nums[1][1] = 6;
nums[1][2] = 7;
nums[1][3] = 8;
nums[2][0] = 9;
nums[2][1] = 10;
nums[2][2] = 11;
nums[2][3] = 12;
nums[3][0] = 13;
nums[3][1] = 14;
nums[3][2] = 15;
nums[3][3] = 16;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
System.out.print(nums[i][j] + " ");
}
System.out.println();
}
}
}
包测试5;
公共类测试5{
公共静态void main(字符串[]args){
int[][]nums=新int[4][4];
nums[0][0]=1;
nums[0][1]=2;
nums[0][2]=3;
nums[0][3]=4;
nums[1][0]=5;
nums[1][1]=6;
nums[1][2]=7;
nums[1][3]=8;
nums[2][0]=9;
nums[2][1]=10;
nums[2][2]=11;
nums[2][3]=12;
nums[3][0]=13;
nums[3][1]=14;
nums[3][2]=15;
nums[3][3]=16;
对于(int i=0;i
所以任何人都可以帮我吗?如果您需要自己动手,请尝试以下方法:
4 3 2 1
16 12 8 4
15 11 7 3
14 10 6 2
13 9 5 1
public class MatrixTranspose {
static int m1[][] = new int[][]{{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}, {13, 14, 15, 16, 17}};
public static String toString(int[][] m) {
StringBuilder text = new StringBuilder();
for (int row = 0; row < m.length; row++) {
int r[] = m[row];
for (int col = 0; col < r.length; col++) {
if (col > 0) text.append(", ");
text.append(r[col]);
}
text.append("\n");
}
return text.toString();
}
public static int[][] transpose(int[][] m) {
int rows = m.length;
int cols = m[0].length;
int t[][] = new int[cols][]; // first create the empty transpose matrix
for (int trow = 0; trow < cols; trow++) {
t[trow] = new int[rows];
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int tcol = rows-row-1; // transposed tcol is inverted row
int trow = cols-col-1; // transposed trow is inverted col
t[trow][tcol] = m[row][col];
}
}
return t;
}
public static void main(String...params) {
System.out.println(toString(m1));
System.out.println("--");
System.out.println(toString(transpose(m1)));
}
}
公共类矩阵传输{
静态int m1[][]=新int[]{{1,2,3,4,5},{5,6,7,8,9},{9,10,11,12,13},{13,14,15,16,17};
公共静态字符串toString(int[]m){
StringBuilder text=新的StringBuilder();
对于(int row=0;row0)文本。追加(“,”);
text.append(r[col]);
}
text.append(“\n”);
}
返回text.toString();
}
公共静态int[][]转置(int[][]m){
int行=m.length;
int cols=m[0]。长度;
int t[][]=new int[cols][];//首先创建空转置矩阵
for(int-trow=0;trow 你可以考虑一下:如果你做更多的矩阵运算…谢谢你的帮助,你能更详细地解释转置函数吗?