Thanks for both the mapply and array approaches! However, although intended to generate the same result, they don't:
# mapply approach n = 3 sa <- rnorm(n,0.8,0.1) so <- rnorm(n,0.5,0.1) m <- rnorm(n,1.2,0.1) mats = mapply(function(sa1, so1, m1) matrix(c(0,sa1*m1,so1,sa1),2,2,byrow=T), sa, so, m, SIMPLIFY = FALSE) print(mats) [[1]] [,1] [,2] [1,] 0.0000000 0.8643679 [2,] 0.4731249 0.7750431 [[2]] [,1] [,2] [1,] 0.0000000 0.8838286 [2,] 0.5895258 0.7880983 [[3]] [,1] [,2] [1,] 0.0000000 1.1491560 [2,] 0.4947322 0.9744166 Now, the array approach: # array approach ms <- array(c(rep(0, 3),sa*m,so,sa), c(3, 2, 2)) for (i in 1:n) { print(ms[i,,]) [,1] [,2] [1,] 0.0000000 0.4731249 [2,] 0.8643679 0.7750431 [,1] [,2] [1,] 0.0000000 0.5895258 [2,] 0.8838286 0.7880983 [,1] [,2] [1,] 0.000000 0.4947322 [2,] 1.149156 0.9744166 These matrices are the transpose of those returned by the mapply approach. To see if one approach or the other is 'confused', I simply rerun setting sd=0 for the parameters -- thus, every matrix will be the same. The correct matrix would be: [,1] [,2] [1,] 0.0 0.96 [2,] 0.5 0.80 In fact, this is what is returned by the mapply approach, while the array approach returns the transpose. I gather the 'missing step' is to use aperm, but haven't figured out how to get that to work...yet. On 9/28/2017 5:11 AM, Duncan Murdoch wrote: > ms <- array(c(rep(0, 5),sa*m,so,sa), c(5, 2, 2)) [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.