lundi 26 février 2018

Select randomly from array with probabilites

Assume I have an array of items [1,2, ...n], and probabilities array [p1,p2,....,pn]

where n: a very large number may reach to thousands and the sum of all probabilities = 1.

I need to select 3 unique items randomly each time, the item with high probability its chance to select is higher. I need to do the selection for more than 20k times.

I've implemented the required by creating new array that contains the items with repetition based on its probability, for example, if probabilities for item1,item2, item3 are [2/n,4/n,1/n] respectively ... then the new array will contain [1,1,2,2,2,2,3, ...]

It works fine but it's not efficient especially in this method the chance to select the same item is possible, then reselect another item and this consumes time.

Is there any efficient methods or ready functions in MATLAB for that purpose.

Thanks,




Aucun commentaire:

Enregistrer un commentaire