Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant "const". The third column is the result of multiplying the second column with "const"..... So far, I did it like this (as a simplified example): nr.of.columns <- 4 myconstant <- 27.5 mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] <- 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] <- myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}
If `v' the the vector in the first column and `k' is the constant, then the matrix has k^(j-1) * v in the jth column, right?> k <- 27.5^seq(0, length=4) > k[1] 1.00 27.50 756.25 20796.88> outer(1:5, k, "*")[,1] [,2] [,3] [,4] [1,] 1 27.5 756.25 20796.88 [2,] 2 55.0 1512.50 41593.75 [3,] 3 82.5 2268.75 62390.63 [4,] 4 110.0 3025.00 83187.50 [5,] 5 137.5 3781.25 103984.38 Andy> From: Rau, Roland > > Dear R-Helpers, > > I have a matrix where the first column is known. The second column is > the result of multiplying this first column with a constant > "const". The > third column is the result of multiplying the second column with > "const"..... > So far, I did it like this (as a simplified example): > > nr.of.columns <- 4 > > myconstant <- 27.5 > > mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) > > mymatrix[,1] <- 1:5 > > for (i in 2:nr.of.columns) { > mymatrix[,i] <- myconstant * mymatrix[,i-1] > } > > > Can anyone give me some advice whether it is possible to > avoid this loop > (and if yes: how)? > > Any suggestions are welcome! > > Thanks, > Roland > > > > > +++++ > This mail has been sent through the MPI for Demographic > Rese...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Does this do what you want? nr.of.columns <- 4 myconstant <- 27.5 mymatrix <- matrix(myconstant, nrow=5, ncol=nr.of.columns) mymatrix[,1] <- 1:5 t(apply(mymatrix, 1, function(x) cumprod(x))) __________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 "Rau, Roland" <Rau at demogr.mpg.de> To: <r-help at stat.math.ethz.ch> Sent by: cc: r-help-bounces at stat.m Subject: [R] Avoiding a Loop? ath.ethz.ch 01/21/2005 07:31 Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant "const". The third column is the result of multiplying the second column with "const"..... So far, I did it like this (as a simplified example): nr.of.columns <- 4 myconstant <- 27.5 mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] <- 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] <- myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}} ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Dear R-List, thank you very much for the fast answers. Andy Liaw and James Holtman both gave me some example code which does exactly what I want. I will now try to check which one is better suited for my actual matrix (in terms of speed). Thanks, Roland -----Original Message----- From: james.holtman at convergys.com [mailto:james.holtman at convergys.com] Sent: Friday, January 21, 2005 3:57 PM To: Rau, Roland Cc: r-help at stat.math.ethz.ch; r-help-bounces at stat.math.ethz.ch Subject: Re: [R] Avoiding a Loop? Does this do what you want? nr.of.columns <- 4 myconstant <- 27.5 mymatrix <- matrix(myconstant, nrow=5, ncol=nr.of.columns) mymatrix[,1] <- 1:5 t(apply(mymatrix, 1, function(x) cumprod(x))) __________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 "Rau, Roland" <Rau at demogr.mpg.de> To: <r-help at stat.math.ethz.ch> Sent by: cc: r-help-bounces at stat.m Subject: [R] Avoiding a Loop? ath.ethz.ch 01/21/2005 07:31 Dear R-Helpers, I have a matrix where the first column is known. The second column is the result of multiplying this first column with a constant "const". The third column is the result of multiplying the second column with "const"..... So far, I did it like this (as a simplified example): nr.of.columns <- 4 myconstant <- 27.5 mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) mymatrix[,1] <- 1:5 for (i in 2:nr.of.columns) { mymatrix[,i] <- myconstant * mymatrix[,i-1] } Can anyone give me some advice whether it is possible to avoid this loop (and if yes: how)? Any suggestions are welcome! Thanks, Roland +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}} ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}
"Rau, Roland" <Rau at demogr.mpg.de> writes:> Dear R-Helpers, > > I have a matrix where the first column is known. The second column is > the result of multiplying this first column with a constant "const". The > third column is the result of multiplying the second column with > "const"..... > So far, I did it like this (as a simplified example): > > nr.of.columns <- 4 > > myconstant <- 27.5 > > mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) > > mymatrix[,1] <- 1:5 > > for (i in 2:nr.of.columns) { > mymatrix[,i] <- myconstant * mymatrix[,i-1] > } > > > Can anyone give me some advice whether it is possible to avoid this loop > (and if yes: how)? >How about:> myconstant <- 27.5 > a <- 1:5 > myconstant <- myconstant^((1:nr.of.columns)-1) > myconstant[1] 1.00 27.50 756.25 20796.88> mymatrix <- outer(a,myconstant) > mymatrix[,1] [,2] [,3] [,4] [1,] 1 27.5 756.25 20796.88 [2,] 2 55.0 1512.50 41593.75 [3,] 3 82.5 2268.75 62390.62 [4,] 4 110.0 3025.00 83187.50 [5,] 5 137.5 3781.25 103984.38>HTH Zoltan> Any suggestions are welcome! > > Thanks, > Roland > > > > > +++++ > This mail has been sent through the MPI for Demographic Rese...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Z. Barta Dept. of Evol. Zool., Univ. of Debrecen, Debrecen, H-4010, Hungary Phone: 36 52 316 666 ext. 2334, Fax: 36 52 533 677 E-mail: zbarta at delfin.unideb.hu, http://puma.unideb.hu/~zbarta/
Rau, Roland wrote:> Dear R-Helpers, > > I have a matrix where the first column is known. The second column is > the result of multiplying this first column with a constant "const". The > third column is the result of multiplying the second column with > "const"..... > So far, I did it like this (as a simplified example): > > nr.of.columns <- 4 > > myconstant <- 27.5 > > mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) > > mymatrix[,1] <- 1:5 > > for (i in 2:nr.of.columns) { > mymatrix[,i] <- myconstant * mymatrix[,i-1] > }nr.of.columns <- 4 myconstant <- 27.5 mycolumn <- 1:5 outer(mycolumn, myconstant^(0:(nr.of.columns-1))) Uwe Ligges> > Can anyone give me some advice whether it is possible to avoid this loop > (and if yes: how)? > > Any suggestions are welcome! > > Thanks, > Roland > > > > > +++++ > This mail has been sent through the MPI for Demographic Rese...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Roland: Andy Liaw and others have already given perfectly good answers to this (but note: Using apply() type functions does **not** avoid loops; apply's **are** loops). However, mostly as an illustration to reinforce Uwe Ligges's comments (in the "dim vs length" thread) about the usefulness of sometimes treating arrays as vectors, I offer the following: nc<-ncol(yourmatrix) matrix(yourmatrix[,1]*rep(k^seq(0,length=nc),e=nrow(yourmatrix)),ncol=nc) Alteratively, one could use matrix multiplication: yourmatrix[,1]%*%matrix(k^seq(0,length=nc),nrow=1) Both of these will be very fast (although unlikely to make a noticeable difference without a lot of rows or columns) and I think use less memory than outer() (again, unlikely to make a noticeable difference). -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Rau, Roland > Sent: Friday, January 21, 2005 4:32 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Avoiding a Loop? > > Dear R-Helpers, > > I have a matrix where the first column is known. The second column is > the result of multiplying this first column with a constant > "const". The > third column is the result of multiplying the second column with > "const"..... > So far, I did it like this (as a simplified example): > > nr.of.columns <- 4 > > myconstant <- 27.5 > > mymatrix <- matrix(numeric(0), nrow=5, ncol=nr.of.columns) > > mymatrix[,1] <- 1:5 > > for (i in 2:nr.of.columns) { > mymatrix[,i] <- myconstant * mymatrix[,i-1] > } > > > Can anyone give me some advice whether it is possible to > avoid this loop > (and if yes: how)? > > Any suggestions are welcome! > > Thanks, > Roland > > > > > +++++ > This mail has been sent through the MPI for Demographic > Rese...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >