[R] How to define new matrix based on an elementary row operation in a single step?

David Winsemius dwinsemius at comcast.net
Sat Aug 28 04:16:47 CEST 2010


On Aug 27, 2010, at 9:21 PM, Cuckovic Paik wrote:

>
>
I'm not absolutely sure I know what you mean by elementary row  
operations ... you are supposed to offer test cases and specify the  
desired output on r-help to support the aging faculties of the helpeRs  
in this case ...  having only the vaguest of memories of my linear  
algebra lessons from the late 60's,  but will exemplify: A)  addition  
of a factor times another row and b) exchanges of rows:

 > tst<-matrix(1:16, 4,4)
 > tst
      [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
 > tst2 <- tst
# note that I kept tst in reserve, so to speak.
 > tst2[2, ] <- tst2[2, ] + 3*tst2[4, ]  #add 3 times row 4 to row 2
 > tst2
      [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]   14   30   46   62
[3,]    3    7   11   15
[4,]    4    8   12   16
 > tst2 <- tst #restore to its original state
 > tst2 <- tst2[c(1,4,3,2), ]  #transpose rows
 > tst2
      [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    4    8   12   16
[3,]    3    7   11   15
[4,]    2    6   10   14

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list