簡體   English   中英

如何在 Android 中對矩陣進行求逆和乘法?

[英]How to invert and multiply matrices in Android?

給定2個矩陣:

public float[] mRi = new float[16];  
public float[] mR = new float[16];  

這些將是兩個讀數的輸出

  • SensorManager.getRotationMatrix(mR, x, y, z)
  • SensorManager.getRotationMatrix(mRi, x, y, z)

所以會有兩個 4x4 矩陣,

我想得到以下等式的結果:

  • ResultMtrix=inverse(mRi)*mR

事實上,我知道它是否適用於invertM()multiplyMM()但我不知道如何使用矩陣來做到這一點。

你能幫我嗎?

嘿家伙,我假設它們是 4x4 矩陣(16 個元素)......您可以使用 Gauss-Jordan 消除http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination進行反轉。 矩陣乘法到處都有描述,甚至比矩陣求逆還要多。

您描述的矩陣實際上是一維向量,所以我假設您所說的inverse實際上是transpose 這種情況下的計算非常簡單:

方法

// 1 row * 1 column
public static float scalarMultiplication (float[] m1, float[] m2) {
    if (m1.length != m2.length)
        throw new IllegalArgumentException("Vectors need to have the same length");
    float m = 0;
    for (int i=0; i<m1.length; i++)
        m += (m1[i]*m2[i]);
    return m;
}

// N rows * N columns
public static float[][] vectorMultiplication (float[] m1, float[] m2) {
    if (m1.length != m2.length)
        throw new IllegalArgumentException("Vectors need to have the same length");
    float[][] m = new float[m1.length][m1.length];
    for (int i=0; i<m1.length; i++)
        for (int j=0; j<m1.length; j++)
            m[i][j] = (m1[i]*m2[j]);
    return m;
}

測試

            float[] m1 = new float[16];
            float[] m2 = new float[16];

            for (int i=0; i<m1.length; i++) {
                m1[i]=i;
                m2[i]=i*i;
            }

            System.out.println ("Multiple is " + scalarMultiplication(m1, m2));
            float[][] m = vectorMultiplication(m1, m2);
            for (int i=0; i<m[0].length; i++) {
                for (int j=0; j<m[0].length; j++) {
                    System.out.print (m[i][j] +" ");
                }
                System.out.println();
            }

Output

Multiple is 14400.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 
0.0 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0 100.0 121.0 144.0 169.0 196.0 225.0 
0.0 2.0 8.0 18.0 32.0 50.0 72.0 98.0 128.0 162.0 200.0 242.0 288.0 338.0 392.0 450.0 
0.0 3.0 12.0 27.0 48.0 75.0 108.0 147.0 192.0 243.0 300.0 363.0 432.0 507.0 588.0 675.0 
0.0 4.0 16.0 36.0 64.0 100.0 144.0 196.0 256.0 324.0 400.0 484.0 576.0 676.0 784.0 900.0 
0.0 5.0 20.0 45.0 80.0 125.0 180.0 245.0 320.0 405.0 500.0 605.0 720.0 845.0 980.0 1125.0 
0.0 6.0 24.0 54.0 96.0 150.0 216.0 294.0 384.0 486.0 600.0 726.0 864.0 1014.0 1176.0 1350.0 
0.0 7.0 28.0 63.0 112.0 175.0 252.0 343.0 448.0 567.0 700.0 847.0 1008.0 1183.0 1372.0 1575.0 
0.0 8.0 32.0 72.0 128.0 200.0 288.0 392.0 512.0 648.0 800.0 968.0 1152.0 1352.0 1568.0 1800.0 
0.0 9.0 36.0 81.0 144.0 225.0 324.0 441.0 576.0 729.0 900.0 1089.0 1296.0 1521.0 1764.0 2025.0 
0.0 10.0 40.0 90.0 160.0 250.0 360.0 490.0 640.0 810.0 1000.0 1210.0 1440.0 1690.0 1960.0 2250.0 
0.0 11.0 44.0 99.0 176.0 275.0 396.0 539.0 704.0 891.0 1100.0 1331.0 1584.0 1859.0 2156.0 2475.0 
0.0 12.0 48.0 108.0 192.0 300.0 432.0 588.0 768.0 972.0 1200.0 1452.0 1728.0 2028.0 2352.0 2700.0 
0.0 13.0 52.0 117.0 208.0 325.0 468.0 637.0 832.0 1053.0 1300.0 1573.0 1872.0 2197.0 2548.0 2925.0 
0.0 14.0 56.0 126.0 224.0 350.0 504.0 686.0 896.0 1134.0 1400.0 1694.0 2016.0 2366.0 2744.0 3150.0 
0.0 15.0 60.0 135.0 240.0 375.0 540.0 735.0 960.0 1215.0 1500.0 1815.0 2160.0 2535.0 2940.0 3375.0 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM