2011-05-27 90 views
0

鉴于2个矩阵:如何在Android中反转和相乘矩阵?

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

这些都将是两个读数的输出从

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

所以会有两个4×4矩阵,其,

我想下面的公式的结果:

  • ResultMtrix=inverse(mRi)*mR

其实我也不管它与invertM()multiplyMM()的想法,但我没有对如何做一个线索它与矩阵。

你能帮忙吗?

+0

你的意思是你不知道如何设置的ES格式的矩阵? – 2011-05-27 02:42:56

+0

你在使用什么库?你如何期待得到1x16(非方形)矩阵的逆矩阵? – bdares 2011-05-27 02:55:16

+0

我没有解释清楚我想要什么,实际上这是getRotationMatrix的输出(mR,float [] z,float [] mGdata,float [] mMData) – Mark 2011-05-27 16:04:12

回答

0

矩阵你所描述的实际上是单维向量,所以我假设你叫什么实际上是。在这种情况下的计算很简单:

方法

// 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(); 
      } 

输出

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