Is this sufficiently single steppish for you? D <- A <- matrix(1:16, 4) D[3, ] <- 2 * D[1, ] + D[3, ]
# Alternately, you could do this # but it is much messier, and I do not see how # two steps is really an issue # you want to end up with two matrices anyways # so it's not like you save memory by only making one assignment A2 <- matrix(1:16, 4) D2 <- rbind(A2[1:2, ], 2 * A2[1, ] + A2[3, ], A2[4, ]) all.equal(A, A2) all.equal(D, D2) Cheers, Josh On Fri, Aug 27, 2010 at 10:32 PM, Cheng Peng <cp...@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. > > > > > > -- > 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-tp2341768p2352538.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.