Pick a number from an array with a certain probability
조회 수: 6 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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개)
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!