2013-10-10 17 views
0

假設我有一個矩陣A.我需要輸入什麼來獲得A的轉置矩陣? (可以說B)如何在java中獲得矩陣/數組的轉置?

我已經在我的項目進口的Apache共享數學,我想使用這些庫做)

我的代碼是:

double[][] A = new double[2][2]; 
     A[0][0] = 1.5; 
     A[0][1] = -2.0; 
     A[1][0] = 7.3; 
     A[1][1] = -13.5; 

那麼,是什麼? ...

(我發現這個鏈接,但我不知道到底該怎麼做:

http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/linear/RealMatrix.html

我曾嘗試:

double[][] a = new double[2][2]; 
a = RealMatrix.transpose(); 

此外,

double[][] a = new double[2][2]; 
a = A.transpose(); 

而且我怎麼能以同樣的方式轉一個數組?

+0

http://stackoverflow.com/questions/15449711/transpose-double-matrix-with-a-java-function看到這個 – vels4j

+0

我上面寫了我的嘗試:) –

+0

@ vels4j 我已經看到這個,但我想使用 Commons Math庫的子例程。 我想這樣做,因爲我經常使用該庫的更多子程序... –

回答

1
double[][] matrixData = { {1.5d,2d}, {7.3d,-13.5d}}; 
RealMatrix m = MatrixUtils.createRealMatrix(matrixData); 

有一種方法叫轉返回矩陣

RealMatrix m1=m.transpose(); 

M1的轉置是m的轉置

+0

我知道這個鏈接(這是我上面提到的那個...) 我的問題是,我怎麼把它在我的程序中? 你能幫忙嗎? :) –

+0

請參閱編輯Konstantinos,m1是轉置。 –

+0

我認爲這是我想要的! :) 謝謝。 –

1

你可以嘗試這樣的:

public void transpose() { 

     final int[][] original = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; 
     for (int i = 0; i < original.length; i++) { 
      for (int j = 0; j < original[i].length; j++) { 
       System.out.print(original[i][j] + " "); 
      } 
      System.out.print("\n"); 
     } 
     System.out.print("\n\n matrix transpose:\n"); 
     // transpose 
     if (original.length > 0) { 
      for (int i = 0; i < original[0].length; i++) { 
       for (int j = 0; j < original.length; j++) { 
        System.out.print(original[j][i] + " "); 
       } 
       System.out.print("\n"); 
      } 
     } 
    }