MATLAB Answers

# Generating a random number from array based on requirements

27 ビュー (過去 30 日間)
Matlab Beginner 2021 年 2 月 24 日
コメント済み: Walter Roberson 2021 年 2 月 24 日
I am using the code below to allow me to select a single "random" element from an array with each element having its own weighting.
A=[1,2,3,4];
p=[10 20 30 40];
c=cumsum(p);
[~,r]=histc(rand(1,1),[0 c/c(end)]);
r=A(r);
Suppose my requirements now change, and only elements 3 and 4 can be selected from A. Without having to redo the percentages (i.e. changing p array), how can I modify the inputs to the histc function to achieve this?
Thank you

サインインしてコメントする。

### 回答 (1 件)

Rik 2021 年 2 月 24 日
A=[1,2,3,4];
p=[10 20 30 40];
c=cumsum(p);
isAllowed=[3,4];
while true
[~,r]=histc(rand(1,1),[0 c/c(end)]);
if ismember(r,isAllowed),break;end
end
disp(r)
4
##### 6 件のコメント表示非表示 5 件の古いコメント
Walter Roberson 2021 年 2 月 24 日
A = [1, 2, 3, 4];
p = [10 20 30 40];
isAllowed=[3,4];
c = cumsum(p);
N = 100;
[~,r]=histc(rand(1,N),[0 c/c(end)]);
r = A(r(ismember(r,isAllowed)));
stem(r) This is the rejection process. Notice that the size of the returned data is only 70% of nominal. If you need a fixed number of samples output then you have to go back and ask for more. This can be pretty expensive -- for example you might be asking to accept only items with a 5% probability, and then to get 100 outputs on average you would need 100/0.05 = 2000 tries.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!