I'm trying to code more R-like and avoid loops. Here is an example I'm having a hard time getting away from loops with, and the real matrices are rather large, and the computation is too time-consuming. ### Dimensions N <- 2 M <- 3 P <- 4 ### Array and Matrices nu <- array(NA,dim=c(N,M,P)) Lambda <- matrix(1:12,P,M) F <- matrix(1:8,N,P) ### Loop to avoid for (i in 1:N) {for (m in 1:M) {for (p in 1:P) { nu[i,m,p] <- Lambda[p,m] * F[i,p] }}} Any help is appreciated. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/How-would-you-avoid-loops-in-this-simple-example-tp3390017p3390017.html Sent from the R help mailing list archive at Nabble.com.
Hi, You are basically just doing matrix multiplication, which R has some built in, optimized functions for. You can see the documentation at ?matmult This gets you part of the way there. I suspect you can even avoid the last loop if you know enough about linear algebra and how to use some clever transposition and inner or outer multiplication (?outer ?t ?matmult). ## alternate nu nu2 <- array(NA,dim=c(N,M,P)) ## avoiding much of that loop for(i in 1:P) { nu2[,, i] <- F[, i, drop = FALSE] %*% Lambda[i, ] } HTH, Josh On Sat, Mar 19, 2011 at 10:27 AM, zerfetzen <paul.heinrich.dietrich at gmail.com> wrote:> I'm trying to code more R-like and avoid loops. Here is an example I'm having > a hard time getting away from loops with, and the real matrices are rather > large, and the computation is too time-consuming. > > ### Dimensions > N <- 2 > M <- 3 > P <- 4 > ### Array and Matrices > nu <- array(NA,dim=c(N,M,P)) > Lambda <- matrix(1:12,P,M) > F <- matrix(1:8,N,P) > ### Loop to avoid > for (i in 1:N) {for (m in 1:M) {for (p in 1:P) { > ? ? nu[i,m,p] <- Lambda[p,m] * F[i,p] > }}} > > Any help is appreciated. Thanks. > > -- > View this message in context: http://r.789695.n4.nabble.com/How-would-you-avoid-loops-in-this-simple-example-tp3390017p3390017.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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/
On Sat, Mar 19, 2011 at 10:27:11AM -0700, zerfetzen wrote:> I'm trying to code more R-like and avoid loops. Here is an example I'm having > a hard time getting away from loops with, and the real matrices are rather > large, and the computation is too time-consuming. > > ### Dimensions > N <- 2 > M <- 3 > P <- 4 > ### Array and Matrices > nu <- array(NA,dim=c(N,M,P)) > Lambda <- matrix(1:12,P,M) > F <- matrix(1:8,N,P) > ### Loop to avoid > for (i in 1:N) {for (m in 1:M) {for (p in 1:P) { > nu[i,m,p] <- Lambda[p,m] * F[i,p] > }}}Try the following Lambda1 <- aperm(array(Lambda, dim=c(P, M, N)), perm=c(3, 2, 1)) F1 <- aperm(array(F, dim=c(N, P, M)), perm=c(1, 3, 2)) nu1 <- Lambda1 * F1 identical(nu, nu1) [1] TRUE The function array() recycles its first argument, if the required dimension of the output is larger. The function aperm() then puts the indices to the required order. Hope this helps. Petr Savicky.
Thanks for the help, and now I have some functions to learn. Both suggestions work great, really appreciate it. -- View this message in context: http://r.789695.n4.nabble.com/How-would-you-avoid-loops-in-this-simple-example-tp3390017p3390549.html Sent from the R help mailing list archive at Nabble.com.