Generate constrained random vector
2 次查看(过去 30 天)
显示 更早的评论
I am trying to generate a random vector from two other random vectors and a scaller in Matlabsuch that λ* P - Φ.*μ < 0 where P, Φ, μ are vectors and λ is a scaler value. I have written following code in Matlab. But it stuck in while loop many times when I try to use higher values of λ e.g. λ = 30 or 50. I shall appreciate any help.
%generate P from uniform distribution: 0≤ P ≤1 and ∑P =1
a=rand(47,1);
a=[0;a;1];
a=sort(a);
b=zeros(48,1);
for i=1:48
b(i)=abs(a(i)-a(i+1));
end
P=b';
%---------------------------------------------
% Generate μ from uniform distribution: 1≤ μ ≤2
%----------------------------------------------
a=1; b=2; miu = a+(b-a)* rand(48,1);
%-----------------------------------------------------------------------
% Generate from uniform distribution: 0≤ Φ ≤1 such that λ* P - Φ.*μ < 0
%-----------------------------------------------------------------------
count=1; lamda=5;
while(count ~= 49)
tPhi = rand(1,1);
c = lamda* P(count) - tPhi.*miu(count);
if(c<0)
Phi(count,1)= tPhi;
count=count+1;
end
end
disp(Phi);
2 个评论
Jan
2017-7-14
Of course this can be an infinite loop:
c = lamda * P(count) - tPhi.*miu(count);
P can have the max value 1, lamda is e.g. 30, tPhi has a max of 1 and miu a max of 2. Then there is no chance the c is negative.
回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!