根据R中qr分解中的列旋转结果,删除矩阵的列和行

时间:2013-12-30 00:25:23

标签: r matrix

我有一个mtrix A,我想从qr分解中提取R矩阵 矩阵A

        > A = matrix(c(1.79, 5.20, 6.33, 0, 4.67, 5.20, 6.33, 12.31, 0, 0.0001, 6.33,12.31, 26.96, 0, 0.0047, 0, 0, 0, 0, 0, 0.00046, 0.001, 0.004, 0, 0.00001), nrow=5)
        > A
         [,1]    [,2]    [,3] [,4]    [,5]
     [1,] 1.79  5.2000  6.3300    0 0.00046
     [2,] 5.20  6.3300 12.3100    0 0.00100
     [3,] 6.33 12.3100 26.9600    0 0.00400
     [4,] 0.00  0.0000  0.0000    0 0.00000
     [5,] 4.67  0.0001  0.0047    0 0.00001
     > qr_A = qr(A)
     > qr_A
     $qr
         [,1]        [,2]        [,3]          [,4] [,5]
   [1,] -9.5980154 -12.5178760 -25.6325332 -0.0032704782    0
   [2,]  0.5417787   7.8706912  15.4816171  0.0021627784    0
   [3,]  0.6595113  -0.3127505  -4.6625524 -0.0012327797    0
   [4,]  0.0000000   0.0000000   0.0000000 -0.0005641954    0
   [5,]  0.4865589   0.9231274   0.0644361  1.0000000000    0

   $rank
   [1] 4

  $qraux
  [1] 1.186497 1.223658 1.997922 1.000000 0.000000
  $pivot
  [1] 1 2 3 5 4

 attr(,"class")

 [1] "qr"

我执行以下代码,但是如何根据q $ pivot和q $ rank为任何矩阵R删除矩阵R的行和列?

  > R = qr.R(qr_A)
  > R
        [,1]       [,2]       [,3]          [,4] [,5]
 [1,] -9.598015 -12.517876 -25.632533 -0.0032704782    0
 [2,]  0.000000   7.870691  15.481617  0.0021627784    0
 [3,]  0.000000   0.000000  -4.662552 -0.0012327797    0
 [4,]  0.000000   0.000000   0.000000 -0.0005641954    0
 [5,]  0.000000   0.000000   0.000000  0.0000000000    0

1 个答案:

答案 0 :(得分:2)

据我所知,你试图从A的全等级子矩阵的QR分解中得到R矩阵。这是正确的吗?在这种情况下,请使用以下解决方案:

idx <- qr_A$pivot[seq.int(qr_A$rank)]
A_full <- A[idx, idx]
qr.R(qr(A_full))
          [,1]       [,2]       [,3]          [,4]
[1,] -9.598015 -12.517876 -25.632533 -0.0032704782
[2,]  0.000000   7.870691  15.481617  0.0021627784
[3,]  0.000000   0.000000  -4.662552 -0.0012327797
[4,]  0.000000   0.000000   0.000000 -0.0005641954

请注意,如果您不需要A_full矩阵,请仅使用

qr.R(qr_A)[seq.int(qr_A$rank), seq.int(qr_A$rank)]
          [,1]       [,2]       [,3]          [,4]
[1,] -9.598015 -12.517876 -25.632533 -0.0032704782
[2,]  0.000000   7.870691  15.481617  0.0021627784
[3,]  0.000000   0.000000  -4.662552 -0.0012327797
[4,]  0.000000   0.000000   0.000000 -0.0005641954