jeudi 29 octobre 2015

Pick a matrix cell according to its probability

I have a 2D matrix of positive real values, stored as follow:

vector<vector<double>> matrix;

Each cell can have a value equal or greater to 0, and this value represents the possibility of the cell to be chosen. In particular, for example, a cell with a value equals to 3 has three times the probability to be chosen compared to a cell with value 1.

I need to select N cells of the matrix (0 <= N <= total number of cells) randomly, but according to their probability to be selected.

How can I do that?

The algorithm should be as fast as possible.




Aucun commentaire:

Enregistrer un commentaire