1

I have a mtrix A, and I would like to extract R matrix from qr decomposition of matrix 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"

I do the following code, but how can I remove the row and column of matrix R according to q$pivot and q$rank for any matrix 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
rose
  • 1,971
  • 7
  • 26
  • 32

1 Answers1

2

As far as I can see, you are trying to get R matrix from a QR decomposition of a full-rank submatrix of A. Is that correct? In that case, use the following solution:

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

Note that if you do not need A_full matrix, take only

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
tonytonov
  • 25,060
  • 16
  • 82
  • 98