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)?

0

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/29719674/… - Generally bsxfun is faster and more efficient than using the equivalent code with repmat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.