Pick a number from an array with a certain probability

6 ビュー (過去 30 日間)
S. B.
S. B. 2020 年 5 月 25 日
コメント済み: S. B. 2020 年 5 月 25 日
Hi!
I have an array from 1 to 150 [1 2 3 ...]
and the corresponding probability [0.001 0.003 0.002 ...]
Now I'd like to pick a random number from the first array with taking into account the respective probability.
Has anyone an idea how to proceed?
Thanks!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 5 月 25 日
編集済み: Ameer Hamza 2020 年 5 月 25 日
If you can use pre-defined functions and you have Statistics and Machine Learning Toolbox, then you can use randsample(): https://www.mathworks.com/help/releases/R2020a/stats/randsample.html. Something like this
pop = 1:150;
prob = % probability values 1x150
y = randsample(pop, 1000, true, prob)
If you don't have the toolbox, then try following FEX packages:
  1 件のコメント
S. B.
S. B. 2020 年 5 月 25 日
I have it, and it worked perfectly, thank you!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 5 月 25 日
Is this homework? Sounds like it. See Wikipedia: https://en.wikipedia.org/wiki/Inverse_transform_sampling
Hint: use cumsum(probabililtyVector) and see if a random number is less than the cdf.
  1 件のコメント
S. B.
S. B. 2020 年 5 月 25 日
No, it's part of my thesis, and I'm working the first time with matlab, that's why it might sounded like a homework.
I've tried it with cumsum already, and with
V (v>=0 & v<0.0027)=0;
V(v>=0.0027 & v<0.0040)=1;
V(v>=0.0040 & v<0.0047)=2;...
but for so many probabilities it is too much work, especially if the probabilities might change again.
But thanks, anyway!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by