0
mu=[1 2 3 4 5]';   
sigma=[1 1 1 1 1]';
N=100;    %Number of samples for each mu
R=normrnd(mu,sigma,?)

Using normrnd, is it possible to generate N samples for each mu value without a loop (such that R will be 5 by 100 matrix)?

ASE
  • 1,702
  • 2
  • 21
  • 29

1 Answers1

3

I don't know normrnd.

With older randn, I would have used something like:

repmat(mu,N,1) + randn(N,length(mu))*diag(sigma)

EDIT

Ah, you want the transpose 5x100, it's

repmat(mu,1,N) + diag(sigma)*randn(length(mu),N)
aka.nice
  • 9,100
  • 1
  • 28
  • 40
  • That works. If I may make a suggestion, consider using `bsxfun` instead of `repmat`: `bsxfun(@plus, mu, randn(N, numel(mu))*diag(sigma));` You can see a comprehensive survey in comparison of performance here: http://stackoverflow.com/questions/29719674/comparing-bsxfun-and-repmat - Generally `bsxfun` is faster and more efficient than using the equivalent code with `repmat`. – rayryeng May 12 '16 at 18:29