I need to make a set of 2000 random numbers between 0 and 1, with a mean of 0.5, with most of the numbers being either high or low (bimodal distrubution with peaks at 0 and 1)

조회 수: 5 (최근 30일)
Basically what the question says. It should look something like this
Though the left side doesn't need to have a higher peak or anything.
Flat distributions are easy - but how do I go about making a vector with 2000 numbers with this type of distribution?

채택된 답변

Roger Stafford
Roger Stafford 2016년 2월 24일
편집: Roger Stafford 2016년 2월 24일
t = 2*rand(1,2000)-1;
x = (sign(t).*sqrt(abs(t))+1)/2;
hist(x,20)
This should approximate your two linear distribution segments. If you use a much higher value than 2000, say 2000000, it will approximate it much more closely. This does not get the spike at the left end. I'll leave that refinement to you.
Added note: You can test the accuracy of the above by replacing the 'rand' call with 'linspace', since the latter will be exactly uniformly distributed.
t = 2*linspace(0,1,2000000)-1;
x = (sign(t).*sqrt(abs(t))+1)/2;
hist(x,20)
  댓글 수: 2
meepimmaduck
meepimmaduck 2016년 2월 24일
Both of the answers I got were perfect, and I'm a tad disappointed in myself for not being able to think of either of them! Must have been tired. I only chose this one as the accepted answer over the other because it's the one I ended up using. Thanks very much.
Amy Wong
Amy Wong 2017년 12월 13일
If I have a set of data and I want to plot a histogram with multiples of 0.1 which is the x-axis in the image. So, if I use your answer, for the hist what do I change to?
x = (sign(COE).*sqrt(abs(COE))+1)/2;
hist(x,??)

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

추가 답변 (1개)

jgg
jgg 2016년 2월 24일
m = randi([0,1],2000,1);
r = abs(randn(2000,1));
r = r - min(r);
r = r./max(r);
num = m;
num(m==0) = 0 + r(m==0);
num(m==1) = 1 - r(m==1);
Something like this should do it, since you don`t really care how they are generated. Basically, generate your integers (0,1) then just add some noise. This is truncated Gaussian noise.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by