20 value from a normal distribution..

4 次查看(过去 30 天)
Hi,
I have mu and sigma (stand deviation) i.e. mean=20 and sigma=4. I am generating data with normal distribution from these parameters.
I need to select 20 values ranges between 8 to 32 from this distribution..How to do so?
Thanks in advance
Mu = 20;
sigma = 4;
N = normrnd(Mu, sigma, 100000, 1);
N=round(N);
hist(N,0:1:60);

采纳的回答

Michael Haderlein
编辑:Michael Haderlein 2015-5-7
First, make sure that there are enough numbers available (with the values you use, this might not be a problem):
lo=8;hi=32;num=20;
while sum(N>=lo & N<=hi)<num
N(N<lo | N>hi)=round(normrnd(Mu,sigma,sum(N<lo | N>hi),1));
end
Then, take the first 20 values which are in the respective interval:
ind=find(N>=lo & N<hi,num,'first');
N(ind)
  5 个评论
Michael Haderlein
Looks good. You don't need to round again when you create q. Also, instead of num/2, you can use p1 again. When finding the indices, you have N<hi instead of N<=hi. The rest should be fine.
joy
joy 2015-5-7
Thank you. I shall make those corrections.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by