Easy question with probability
显示 更早的评论
Hi,
Let's say
P = [0.1 0.3 0.4 0.2]
X = [1 2 3 4]
where P(n) is the probability to select the X(n) element. I wish to make a function that select an "random" element of X according to its probability, like
f = myfun(P,X)
>> f = 2
thx a lot
采纳的回答
更多回答(1 个)
Roger Stafford
2017-3-18
Also a little late, this also works:
C = cumsum(P);
f = X(1+sum(C(end)*rand>C));
类别
在 帮助中心 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!