Nice one - didn't think of the array at all as my brain was fixated on lists...
Thanks,
Murali

> Date: Fri, 24 May 2013 21:54:22 +0100
> From: ruipbarradas@sapo.pt
> To: feanor0@hotmail.com
> CC: r-help@r-project.org
> Subject: Re: [R] matrix of random variables from a matrix of means and matrix of sd
> 
> Hello,
> 
> The first instruction doesn't call rnorm just once but it seems more 
> appropriate for your problem;
> The second instruction calls rnorm just once.
> 
> 
> set.seed(59187)
> res1 <- replicate(10, matrix(rnorm(4, mean = a, sd = b), ncol = 2))
> 
> set.seed(59187)
> res2 <- array(rnorm(40, mean = a, sd = b), dim = c(2, 2, 10))
> 
> identical(res1, res2)  # TRUE
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 24-05-2013 20:22, M M escreveu:
> > folks,
> > if i have a matrix of means:
> > a <- matrix(1:4, 2)
> > and a matrix of std deviations:
> > b <- matrix(5:8, 2)
> > and i want to create a matrix X of random variates such that X[i, j] is a draw from normal distribution with mean = a[i, j] and std dev =  b[i, j], i think i can do this?
> > X <- matrix(rnorm(4, mean = a, sd = b), ncol = 2)
> > now if I want to create 10 such matrices, I could possibly do this:
> > lapply(1:10, function(x) matrix(rnorm(4, mean = a, sd = b), ncol = 2))
> > is there a better way to do this with perhaps just one call to the rnorm() function?
> > cheers,
> > murali 		 	   		
> > 	[[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@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.
> >
 		 	   		  
	[[alternative HTML version deleted]]

