Hi,
Let's say
P = [0.1 0.3 0.4 0.2]
X = [1 2 5 9]
where P(n) is the probability to select the X(n) element. I wish to make a function that select a "random" element of X according to its probability, like
f = myfun(P,X)
>> f = 2 (occurs around 30%)
thx a lot

4 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 12 月 7 日
編集済み: Walter Roberson 2016 年 11 月 3 日
This is a double post. See my comment on Andrei's answer: http://www.mathworks.com/matlabcentral/answers/23319-easy-question-with-probability
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2016 年 11 月 3 日
編集済み: Nikolas Spiliopoulos 2016 年 11 月 3 日
what if we have two numbers? Lets say 50% probability to get a=0.05 and b= -0.05
Sorry I am a very beginner in MATLAB
thanks in advance
Nikolas
Walter Roberson
Walter Roberson 2016 年 11 月 3 日
AB = [a,b];
AB( randi([1 2]) )
PANTHAGADA ANIL KUMAR
PANTHAGADA ANIL KUMAR 2020 年 4 月 16 日
how to select an element with least probablility

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

 採用された回答

Sean de Wolski
Sean de Wolski 2011 年 12 月 7 日

2 投票

f = X(find(rand<cumsum(P),1,'first'))

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 12 月 7 日
The answers in the other thread took care in case cumsum(P) < 1 as can happen due to round-off error.

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

その他の回答 (3 件)

Jonathan
Jonathan 2018 年 9 月 3 日
編集済み: Jonathan 2018 年 9 月 3 日

8 投票

The accepted answer is not doing any sanity check, and is sensitive to rounding errors. You should use randsample instead.
To sample n points from X, with replacement, and probabilities P:
randsample( X, n, true, P )
This can also be used with a custom RandStream (see documentation). Be aware that this function does NOT check for negative values in P; check manually if needed.

4 件のコメント

Sepehr Saadatmand
Sepehr Saadatmand 2019 年 10 月 11 日
That was what I was looking for. Thank you !
PANTHAGADA ANIL KUMAR
PANTHAGADA ANIL KUMAR 2020 年 4 月 16 日
how to select an element with least probablility
krishna teja
krishna teja 2020 年 4 月 20 日
searched a lot for this kind of function
Real User
Real User 2023 年 4 月 28 日
randsample() seems to require Statistics and Machine Learning Toolbox

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

Steven Lord
Steven Lord 2020 年 4 月 16 日

1 投票

You can use discretize (which didn't exist when this question was asked originally) to do this. Generate uniform random numbers, bin them using bins whose widths are given by P, and for each bin return the corresponding element of X.
P = [0.1 0.3 0.4 0.2];
X = [1 2 5 9];
values = discretize(rand(1, 1e4), cumsum([0 P]), X);
histogram(values, 'Normalization', 'probability')
The probabilities shown in the histogram should agree pretty closely with the values in P.
Mendi
Mendi 2021 年 7 月 9 日

1 投票

The fastest one (100ns-200ns):
function [idx] = get_random_choice(p)
% Random choice with probability
% Example: get_random_choice([0.2,0.7,0.1])
N=length(p); idx=1; cump=0;
r=rand;
while(idx<N)
cump=cump+p(idx);
if(cump>r),break,else,idx=idx+1;end
end
end

質問済み:

2011 年 12 月 7 日

コメント済み:

2023 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by