[R] whole object approach for nested loops

Richard M. Heiberger rmh at temple.edu
Tue Sep 12 23:29:54 CEST 2006


you are looking for the outer product

?outer


> a <- 1:6
> cc <- matrix(1:6, 3, 2)


> e <- matrix(0, 6,3)
> for (j in 1:3) e[,j] <- a*cc[j,2]
> e
     [,1] [,2] [,3]
[1,]    4    5    6
[2,]    8   10   12
[3,]   12   15   18
[4,]   16   20   24
[5,]   20   25   30
[6,]   24   30   36


> a %o% cc[,2]
     [,1] [,2] [,3]
[1,]    4    5    6
[2,]    8   10   12
[3,]   12   15   18
[4,]   16   20   24
[5,]   20   25   30
[6,]   24   30   36
> >



More information about the R-help mailing list