数组的非互斥概率选取。

7 views (last 30 days)
刚开始用matlab,求解答。。数组P中的数值的大小即为概率值,例如P=[0.1,0.5,0.4],从P中进行一次随机选取,选取按概率,但选出的数不一定为一个,比如例子中三个数都被选取的概率便是0.1*0.5*0.4,这种非互斥的概率选取如何编代码呢,数据量不算少。

Accepted Answer

百家乐注册【www.xbs3512.com】
直接用三个独立的[0,1]之间的均匀随机数(RandomCoin ),分别决定是否抽选P的三个元素就是了
随机数不大于P的某一元素时,即事件落在“应当抽中该元素”上,否则事件落在“不抽选该元素”上,于是改元素被抽中的概率就是它自身的数值。每个随机数都是彼此独立的,故抽中某两个的概率即是这两个元素的乘积,同时抽中三个则是三个元素的乘积。
输出为Output。
P = [0.1,0.5,0.4];
RandomCoin = rand(1,3);
Output = [( RandomCoin(1) <= P(1) ).*P(1),...
    ( RandomCoin(2) <= P(2) ).*P(2),...
    ( RandomCoin(3) <= P(3) ).*P(3)];
Output(Output==0) = [];
disp( RandomCoin )
disp( Output )

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!