无论在python和R中转置都很简单,在java中转置,暂时也找不到现成的函数
下面根据转置的原理简单的实现试试,
package excise;
import java.util.Random;
import java.util.Arrays;
public class T019 {
public static void main(String args[]) {
Random t = new Random();
int test[][] = new int[4][5];
for (int i = 0; i < test.length; i++) {
for (int j = 0; j < test[i].length; j++) {
test[i][j] = t.nextInt(50);
}
}
System.out.println("转置之前的矩阵值是:");
for (int i = 0; i < test.length; i++) {
System.out.println("\n");
for (int j = 0; j < test[i].length; j++) {
System.out.print(test[i][j]+" ");
}
}
System.out.println("\n转置之后的矩阵值是:");
int xx[][]=tfunction(test);
int m=test.length;
int n=test[0].length;
for(int i=0;i<n;i++){
System.out.println("\n");
for(int j=0;j<m;j++){
System.out.print(xx[i][j]+" ");
}
}
}
public static int[][] tfunction(int[][] test){
int m=test.length;
int n=test[0].length;
int t[][]=new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
t[i][j]=test[j][i];
}
}
return t;
}
}