Re: [R] whole object approach for nested loops

From: Richard M. Heiberger <rmh_at_temple.edu>
Date: Tue 12 Sep 2006 - 21:29:54 GMT


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

> >



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 and provide commented, minimal, self-contained, reproducible code. Received on Wed Sep 13 08:42:26 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 12 Sep 2006 - 23:30:04 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.