How to generate a random data vector that follows a constraint
1 回表示 (過去 30 日間)
古いコメントを表示
I want to generate random data vector
where
that follows this constrainst.
where
and
is a constant. I would appreciate any help.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240449/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240450/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240451/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240454/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240452/image.png)
0 件のコメント
採用された回答
Bruno Luong
2019 年 9 月 29 日
編集済み: Bruno Luong
2019 年 9 月 29 日
This will generate uniform distribution (within to polytope)
N = 10;
Etotal = 0;
E = -log(rand(N+1,1));
E = E(1:N,:)./sum(E,1);
2 件のコメント
Bruno Luong
2019 年 9 月 29 日
編集済み: Bruno Luong
2019 年 9 月 29 日
Here is a comparison of distribution with the two other methods proposed below to show the issue of non-uniformity if one doesn't pay attention
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240343/image.png)
N = 2;
Etotal = 1;
p = 3e3;
Ebias = rand(1,p) .* randfixedsum(N,p,Etotal,0,Etotal);
E = rand(1,p).^(1/N)*Etotal .* randfixedsum(N,p,1,0,1);
E2 = -log(rand(N+1,p)); E2 = E2(1:N,:)./sum(E2,1);
subplot(2,2,1)
plot(Ebias(1,:),Ebias(2,:),'.');
title('rand * randfixedsum')
axis equal
subplot(2,2,2)
plot(E(1,:),E(2,:),'.');
axis equal
title('sqrt(rand) * randfixedsum')
subplot(2,2,3)
plot(E2(1,:),E2(2,:),'.');
axis equal
title('exponential method')
その他の回答 (1 件)
Walter Roberson
2019 年 9 月 29 日
Look in the File Exchange for Roger's randfixedsum(). Generate a vector with fixed sum
. Multiply all of the elements by rand() to implement the <= part. (Though you might want to worry about the difficulty that rand() is never exactly 1, so if you generate a sum exactly equal to
and multiply by rand() then the result can never exactly total ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240335/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240333/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240334/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240335/image.png)
3 件のコメント
Walter Roberson
2019 年 9 月 29 日
I was thinking of
rand() * randfixedsum(N,1,Etotal,0,Etotal)
but your comment might still apply.
Bruno Luong
2019 年 9 月 29 日
編集済み: Bruno Luong
2019 年 9 月 29 日
Both give the same non-uniform pdf
The "correct" one is
E = rand()^(1/N)*Etotal * randfixedsum(N,1,1,0,1);
参考
カテゴリ
Help Center および File Exchange で Linear Least Squares についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!