Generate constrained random vector

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 个评论

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.
λ* P - Φ.*μ < 0 is one of the constraints in a research problem I am working on. I have found that if I create P by
x=rand(48,1); P=x./sum(x)
The problem of infinite loops occurs very less. But I do not want to do that way as data generated is probably not uniform. Secondly, if I increase 'u', it can work but I do not know whether that is a practically sound idea.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Random Number Generation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by