Search code examples
matlabrandomprobability

how to randomly select an element from an array having the least probability


how to randomly select an element from an array having the least probability in matlab

example :

      A = [ 1,2,3,4,5,6]

      P = [ 0.01,0.2,0.25.0.2,0.25,0.09] % probability %

Solution

  • Find all indices of the minimum probabilities. Then generate a random interger depending on the number of equally minimum probabilities. Then use matrix indexing to extract a random index of minimum probability and then its corresponding element.

    [~, ind] = mink(P,2); %All indices of minimum elements
    Result = A(inds(randi(numel(inds))));   %Random minimum element