[R] How would you avoid loops in this simple example?

zerfetzen paul.heinrich.dietrich at gmail.com
Sat Mar 19 18:27:11 CET 2011


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.



More information about the R-help mailing list