Random Weighted Selection

版本 1.0.0.0 (1.6 KB) 作者: Adam Gripton
Randomly pick n from m>=n elements, biased with given linear weights without replacement.
284.0 次下载
更新时间 2012/3/21

查看许可证

Randomly pick n from size(h)>=n
elements, biased with linear weights as
given in h, without replacement.

Works with infinity and zero weighting
entries, but always picks them
sequentially in this case.

Syntax: Y=randweightedpick(h,n)

Example:

randweightedpick([2,0,5,Inf,3,Inf,0],7)

returns [4,6,...,2,7] all the time as
[4,6] are infinities and [2,7] are
zeros; in between, the results will be
a permutation of [1,3,5]:

[...,3,5,1,...] 30.0% of the time {5/(2+3+5) * 3/(2+3)}

[...,3,1,5,...] 20.0% of the time {5/(2+3+5) * 2/(2+3)}

[...,5,3,1,...] 21.4% of the time {3/(2+3+5) * 5/(2+5)}

[...,1,3,5,...] 12.5% of the time {2/(2+3+5) * 5/(3+5)}

[...,5,1,3,...] 8.6% of the time {3/(2+3+5) * 2/(2+5)}

[...,1,5,3,...] 7.5% of the time {2/(2+3+5) * 3/(3+5)}

Y returns the vector of indices
corresponding to the weights in h.

引用格式

Adam Gripton (2024). Random Weighted Selection (https://www.mathworks.com/matlabcentral/fileexchange/35790-random-weighted-selection), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2012a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0.0