Cuckovic Paik
2010-Aug-28 01:21 UTC
[R] How to define new matrix based on an elementary row operation in a single step?
-- View this message in context: http://r.789695.n4.nabble.com/How-to-define-new-matrix-based-on-an-elementary-row-operation-in-a-single-step-tp2341768p2341768.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Aug-28 02:16 UTC
[R] How to define new matrix based on an elementary row operation in a single step?
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
Cuckovic Paik
2010-Aug-28 04:29 UTC
[R] How to define new matrix based on an elementary row operation in a single step?
Thanks for respose. you still used two steps to get the new matrix tst2: step 1: tst2 = tst step 2: perform the row operation in tst2. Can you do this in a single step? A similar example:> 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 # I define a new matrix by deleting the first row and the first column of matrix tst using following single step:> New.tst=tst[-1,-1] > New.tst[,1] [,2] [,3] [1,] 6 10 14 [2,] 7 11 15 [3,] 8 12 16 # the original matrix tst is still .> 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 -- View this message in context: http://r.789695.n4.nabble.com/How-to-define-new-matrix-based-on-an-elementary-row-operation-in-a-single-step-tp2341768p2344456.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2010-Aug-28 11:27 UTC
[R] How to define new matrix based on an elementary row operation in a single step?
On Sat, Aug 28, 2010 at 1:32 AM, Cheng Peng <cpeng at usm.maine.edu> wrote:> > Sorry for possible misunderstanding: > > I want to define a matrix (B) based on an existing matrix (A) in a single > step and keep A unchanged: > >> #Existing matrix >> A=matrix(1:16,ncol=4) >> A > ? ? [,1] [,2] [,3] [,4] > [1,] ? ?1 ? ?5 ? ?9 ? 13 > [2,] ? ?2 ? ?6 ? 10 ? 14 > [3,] ? ?3 ? ?7 ? 11 ? 15 > [4,] ? ?4 ? ?8 ? 12 ? 16 >> # New matrix B is defined to be the submatrix after row1 and column1 are >> deleted. >> B=A[-1,-1] ? ?# this single step deletes row1 nad column 1 and assigns the >> name to the resulting submatrix. >> B ? ? ? ? ? ? ? ? # check the new matrix B > ? ? [,1] [,2] [,3] > [1,] ? ?6 ? 10 ? 14 > [2,] ? ?7 ? 11 ? 15 > [3,] ? ?8 ? 12 ? 16 >> A ? ? ? ? ? ? ? ? # check the original matrix A > ? ? [,1] [,2] [,3] [,4] > [1,] ? ?1 ? ?5 ? ?9 ? 13 > [2,] ? ?2 ? ?6 ? 10 ? 14 > [3,] ? ?3 ? ?7 ? 11 ? 15 > [4,] ? ?4 ? ?8 ? 12 ? 16 > > > Question: How can I do define a new matrix (D) by adding 2*row1 to row3 in A > in a single step as what was done in the above example? > > If you do: ?A[3,]=2*A[1,]+A[3,], ?the new A is not the original A; if you > D=A first, then D[3,]=2*D[1,]+D[3,], you used two step! > > Hope this clarifies my original question. Thanks again. >Try using replace: D <- replace(A, row(A) == 3, 2 * A[1,] + A[3,]) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Cuckovic Paik
2010-Aug-28 13:15 UTC
[R] How to define new matrix based on an elementary row operation in a single step?
Thank all help help. Ted's intuitive single step definition is what I want. I try to teach elementary Linear Algebra using R to manupilate matrices. Since my students have no programming experience at all, any fancy and muliple step definition in matrix row operation will confuse them. Again, I appreciate the suggestions from all of you! -- View this message in context: http://r.789695.n4.nabble.com/How-to-define-new-matrix-based-on-an-elementary-row-operation-in-a-single-step-tp2341768p2362791.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Aug-28 16:20 UTC
[R] How to define new matrix based on an elementary row oper
On Aug 28, 2010, at 11:32 AM, Cuckovic Paik wrote:> > Thank you very much, David; > for row swapping: R2<==>R3 > >> A=diag(1:4) >> A > [,1] [,2] [,3] [,4] > [1,] 1 0 0 0 > [2,] 0 2 0 0 > [3,] 0 0 3 0 > [4,] 0 0 0 4 >> A1=A[c(1,3,2,4),] >> A1 > [,1] [,2] [,3] [,4] > [1,] 1 0 0 0 > [2,] 0 0 3 0 > [3,] 0 2 0 0 > [4,] 0 0 0 4Yes, obviously. My point was illustration of construction of equivalent matrix operators (which was what I assumed was the educational goal motivating your insistence on a one step process for a translation operation, but maybe your goal was merely compact code.) The manipulation of indices does not generalize to rotations very well, either. -- David.
Cuckovic Paik
2010-Sep-12 15:27 UTC
[R] How to define new matrix based on an elementary row oper
I appreciate all you help. This is only for instructional purpose: A = matrix(c(0,1,1,-2,-3,1,2,-1,0,2,2,4,1,-3,-2,1,-4,-7,-1,-19), ncol=5, byrow=T) B =matrix(sample(c(0,1,1,-2,-3,1,2,-1,0,2,2,4,1,-3,-2,1,-4,-7,-1,-19),), ncol=5, byrow=T) Which print func( A, B, A+B) can print the resulting matrices A and B and A+B in the following format? [,1] [,2] [,3] [,4] [,5] [,1] [,2] [,3] [,4] [,5] [,1] [,2] [,3] [,4] [,5] [1,] 0 1 1 -2 -3 [1,] 2 -1 0 2 1 [1,] 2 0 1 0 -2 [2,] 1 2 -1 0 2 + [2,] 1 -4 2 -2 -2 = [2,] 2 -2 1 -2 0 [3,] 2 4 1 -3 -2 [3,] -3 1 -7 1 -1 [3,] -1 5 -6 -2 -3 [4,] 1 -4 -7 -1 -19 [4,] -3 0 4 -19 1 [4,] -2 -4 -3 -20 -18 -- View this message in context: http://r.789695.n4.nabble.com/How-to-define-new-matrix-based-on-an-elementary-row-operation-in-a-single-step-tp2341768p2536409.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Sep-12 16:24 UTC
[R] How to print matrices in standard format was ... Re: How to define new matrix based on an elementary row oper
On Sep 12, 2010, at 11:27 AM, Cuckovic Paik wrote:> > I appreciate all you help. This is only for instructional purpose: > > A = matrix(c(0,1,1,-2,-3,1,2,-1,0,2,2,4,1,-3,-2,1,-4,-7,-1,-19), > ncol=5, > byrow=T) > B > =matrix(sample(c(0,1,1,-2,-3,1,2,-1,0,2,2,4,1,-3,-2,1,-4,-7,-1,-19),), > ncol=5, byrow=T) > > Which print func( A, B, A+B) can print the resulting matrices A and > B and > A+B in the following format? > > [,1] [,2] [,3] [,4] [,5] [,1] [,2] [,3] [,4] [,5] > [,1] [,2] [,3] [,4] [,5] > [1,] 0 1 1 -2 -3 [1,] 2 -1 0 2 > 1 [1,] > 2 0 1 0 -2 > [2,] 1 2 -1 0 2 + [2,] 1 -4 2 -2 -2 > = [2,] > 2 -2 1 -2 0 > [3,] 2 4 1 -3 -2 [3,] -3 1 -7 1 > -1 [3,] > -1 5 -6 -2 -3 > [4,] 1 -4 -7 -1 -19 [4,] -3 0 4 -19 1 > [4,] -2 > -4 -3 -20 -18 >for( i in 1:nrow(A) ) { cat(sprintf("%4.0f", A[i, ]), paste(" ",if( i==3 ){"+"}else{" "}, " ", sep=""),sprintf("%4.0f",B[i, ]), paste(" ",if( i==3 ){"="}else{" "}, " ", sep=""), sprintf("%4.0f", (A +B)[i, ]), "\n" )} -- David Winsemius, MD West Hartford, CT
David Winsemius
2010-Sep-12 22:46 UTC
[R] How to print matrices in standard format was ... Re: How to define new matrix based on an elementary row oper
On Sep 12, 2010, at 12:24 PM, David Winsemius wrote:> > On Sep 12, 2010, at 11:27 AM, Cuckovic Paik wrote: > >> >> I appreciate all you help. This is only for instructional purpose: >> >> A = matrix(c(0,1,1,-2,-3,1,2,-1,0,2,2,4,1,-3,-2,1,-4,-7,-1,-19), >> ncol=5, >> byrow=T) >> B >> = >> matrix(sample(c(0,1,1,-2,-3,1,2,-1,0,2,2,4,1,-3,-2,1,-4,-7,-1,-19),), >> ncol=5, byrow=T) >> >> Which print func( A, B, A+B) can print the resulting matrices A >> and B and >> A+B in the following format? >> >> [,1] [,2] [,3] [,4] [,5] [,1] [,2] [,3] [,4] [, >> 5] [,1] [,2] [,3] [,4] [,5] >> [1,] 0 1 1 -2 -3 [1,] 2 -1 0 2 >> 1 [1,] 2 0 1 0 -2 >> [2,] 1 2 -1 0 2 + [2,] 1 -4 2 -2 -2 >> = [2,] 2 -2 1 -2 0 >> [3,] 2 4 1 -3 -2 [3,] -3 1 -7 1 >> -1 [3,] -1 5 -6 -2 -3 >> [4,] 1 -4 -7 -1 -19 [4,] -3 0 4 -19 >> 1 [4,] -2 -4 -3 -20 -18 >> > > for( i in 1:nrow(A) ) { cat(sprintf("%4.0f", A[i, ]), paste(" > ",if( i==3 ){"+"}else{" "}, " ", sep=""),sprintf("%4.0f",B[i, ]), > paste(" ",if( i==3 ){"="}else{" "}, " ", sep=""), sprintf("%4.0f", > (A+B)[i, ]), "\n" )} >for( i in 1:nrow(A) ) { cat(sprintf("%4.0f", A[i, ]), paste(" ",if( i==3 ){"+"}else{" "}, " ", sep=""), sprintf("%4.0f",B[i, ]), paste(" ",if( i==3 ){"="}else{" "}, " ", sep=""), sprintf("%4.0f", (A+B)[i, ]), "\n" )} Even with the prettier printing it stilled seemed like a hack, so here is a grid graphics solution that gives prettier _output_: require(grid) > grid.newpage() > pushViewport(plotViewport(c(5,4,2,2))) # implicit limits are c(0,0,1,1) within plot area > for (i in 1:nrow(A)) { for (j in 1:ncol(A)){grid.text(A[i,j], x=i/ 20, y=j/20)}} # plot mtx A > for (i in 1:nrow(B)) { for (j in 1:ncol(B)){grid.text(B[i,j], x=(i +5)/20, y=j/20)}} # B > for (i in 1:nrow(B)) { for (j in 1:ncol(B)){grid.text(A[i,j] +B[i,j], x=(i+10)/20, y=j/20)}} # A+B > grid.text("=", x=10/20, y=2.5/20) > grid.text("+", x=5/20, y=2.5/20)>-- David Winsemius, MD West Hartford, CT
Cuckovic Paik
2010-Sep-13 00:32 UTC
[R] How to print matrices in standard format was ... Re: How to define new matrix based on an elementary row oper
Thanks David. grid graphic works pretty well. -- View this message in context: http://r.789695.n4.nabble.com/How-to-define-new-matrix-based-on-an-elementary-row-operation-in-a-single-step-tp2341768p2536785.html Sent from the R help mailing list archive at Nabble.com.