Dear, I would like to generate random nmbers with a normal distribution. From the documentation I see that there are two functioncs that seem to make the same things. normrnd(mu,sigma) and random('Normal',mu,sigma)
I wrote these two simpole for-loop in order to evaluate time consumption of these two functions and I found that normrnd is less time-consuming than random. I don't understand this difference. Could you help me?
tic for i=1:1000000 ciao=ciao+normrnd(0,0.01); end toc
tic for i=1:1000000 ciao=ciao+random('Normal',0,0.01); end toc
Kind regards
Andrea Giostri

 채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 6월 20일

0 개 추천

Hi,
if you open random with an editor and search for "normrnd", you will see, that random in fact calls normrnd. That's why it takes a little more time. On the other hand: if you call normrnd and random one time with N=1000000 instead of N calls in a loop, it won't make a difference at all ...
Titus

댓글 수: 4

Andrew Newell
Andrew Newell 2011년 6월 20일
There is also randn, which is slightly faster than either of the alternatives:
N = 10000;
mu = 0;
sigma = 0.01;
clear ciao
tic
ciao = sigma*sum(randn(N));
toc
clear ciao
tic
ciao = sum(normrnd(mu,sigma,N));
toc
clear ciao
tic
ciao = sum(random('Normal',mu,sigma,N));
toc
Elapsed time is 2.800175 seconds.
Elapsed time is 3.497521 seconds.
Elapsed time is 3.473260 seconds.
However, when I tried to increase N to 100000, my computer ran out of memory (and it has 12 GB).
Titus Edelhofer
Titus Edelhofer 2011년 6월 20일
Hi Andrew,
common mistake I often do as well: randn(N) is not randn(N,1) but randn(N,N). I'm not surprised this will blow your memory with N=100000 ;-).
Titus
Andrew Newell
Andrew Newell 2011년 6월 20일
Aaagh! Right at the top of http://www.mathworks.com/matlabcentral/answers/1759-dumb-mistakes-we-make-with-matlab! Thanks for reminding me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Climate Science and Analysis에 대해 자세히 알아보기

질문:

2011년 6월 20일

댓글:

2022년 6월 11일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by