フィルターのクリア

Vecorize NORMRND for a family of distributions

3 ビュー (過去 30 日間)
John Adcox
John Adcox 2014 年 10 月 30 日
コメント済み: John Adcox 2014 年 11 月 3 日
Hey everyone!
So, I am looking for a way to speed up my code. I have a large vector of normal distributions (i.e. a vector of means and standard deviations) that I need to generate random numbers from. A generic example of my code looks like this:
tic
N=1e6;
mu = rand(N,1);
sigma = rand(N,1);
temp = zeros(length(mu),1);
for i = 1:length(mu)
temp(i) = normrnd(mu(i),sigma(i));
end
toc
This code in its current form has an elapsed time of:
Elapsed time is 12.281509 seconds.
I normally try to vectorize most of computationally intensive commands, but right now I am stumped as to how I can make this run faster. I will have to perform this operation multiple times every time that the code is run, so the faster I can make it the better.
Do any of you MATLAB geniuses out there have any thoughts of how to speed this up?
Thanks! John

採用された回答

Ilya
Ilya 2014 年 11 月 3 日
Replace
temp = zeros(length(mu),1);
for i = 1:length(mu)
temp(i) = normrnd(mu(i),sigma(i));
end
with
temp = normrnd(mu,sigma);
  1 件のコメント
John Adcox
John Adcox 2014 年 11 月 3 日
Thank you llya. This does work. Apparently, for some reason my mind was not working clearly earlier and I missed this simple solution.
Another solution proposed elsewhere would be to do the following:
N=1e6;
mu = rand(N,1);
sigma = rand(N,1);
temp = randn(size(sigma)).*sigma + mu;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by