"Rau, Roland" <[EMAIL PROTECTED]> 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@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: [EMAIL PROTECTED], http://puma.unideb.hu/~zbarta/ ______________________________________________ R-help@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