有没有人有一个函数可以在 Java 中转置具有以下形式的矩阵:
double[][]
我有这样的功能:
public static double[][] transposeMatrix(double [][] m){
for (int i = 0; i < m.length; i++) {
for (int j = i+1; j < m[0].length; j++) {
double temp = m[i][j];
m[i][j] = m[j][i];
m[j][i] = temp;
}
}
return m;
}
但它在某处不对。
最佳答案
public static double[][] transposeMatrix(double [][] m){
double[][] temp = new double[m[0].length][m.length];
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
temp[j][i] = m[i][j];
return temp;
}
关于java - 用 java 函数转置 double[][] 矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15449711/