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일
편집: Michael Haderlein 2015년 5월 7일

0 개 추천

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

Hi, Thank you.
ind=find(N>=lo & N<hi,num,'first');
can you please, let me know the significance of 'first' word... if I need another set of 20 values from this distribution as well...where there shall be changes?
'first' means the first num indices are used. If you rather want the last num indices, use 'last' instead. If you want, let's say, 3x num values, you can set the respective parameter in the find function to 3*num (instead of only num). Then take N(1:num), N(num+1:2*num) and N(2*num+1:3*num).
joy
joy 2015년 5월 7일
편집: joy 2015년 5월 7일
p1=20;
lo=8;hi=32;num=2*p1;
while sum(N>=lo & N<=hi)<num
N(N<lo | N>hi)=round(normrnd(Mu,sigma,sum(N<lo | N>hi),1));
end
ind=find(N>=lo & N<hi,num,'first');
q=round(N(ind));
q=q';
w1=q(1:num/2); %%%%%one set of 20 values
h1=q(num/2+1:end); %%%%%another set of 20 values
is it correct way? Did I get u correctly?
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개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

질문:

joy
2015년 5월 7일

댓글:

joy
2015년 5월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by