How can I make an Gaussian random array with a constraint?

조회 수: 3 (최근 30일)
shima rashidi
shima rashidi 2014년 11월 5일
댓글: Torsten 2014년 11월 5일
I'm trying to create an array with size of nPopulation=100. I want all the values to be driven from the standard normal distribution and positive. but sum of the values should be landaT=25000. I've written the code below
flag=0;
while (flag==0)
landa=(randn(1,nPopulation))*(landaT./nPopulation);
landa(1,nPopulation)=landaT-sum(landa)-landa(1,nPopulation);
if sum (landa)==landaT
flag=1;
end
end
But it doesn't seem to work. If you have any better Ideas, it would be great to hear from.

답변 (1개)

Orion
Orion 2014년 11월 5일
편집: Orion 2014년 11월 5일
Your criteria is too strict.
it's allways risky to compare 2 doubles, because of the numerical precision.
yo should do something like :
flag=0;
landaT = 25000;
nPopulation=100;
NbIter = 0;
while (flag==0)
NbIter = NbIter + 1;
landa=(randn(1,nPopulation))*(landaT./nPopulation);
landa(1,nPopulation)=landaT-sum(landa)-landa(1,nPopulation);
if abs(sum(landa)-landaT)<0.01
flag=1;
end
end
disp('Number of Iteration to convergence');NbIter
Here, you compare the difference of the 2 parameters you want, in absolute value, to a epsilon you defined (0.01)
  댓글 수: 2
shima rashidi
shima rashidi 2014년 11월 5일
Thanks for the answer, its helpful. but my another problem is making the values positive. how can I make them positive and be sure that they still are Gaussian rand numbers.
Torsten
Torsten 2014년 11월 5일
You can't because the standard normal distribution takes positive and negative values.
Maybe you mean
or
Best wishes
Torsten.

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

카테고리

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