MATLAB: Bucketing values

bucketingdistributionmappingMATLAB

Hi
I have a vector of values from 0 to 1 representing probabilities
A = [0.8756 0.1185 0.0059]
How can i generate a random number using rand() and map it to a value in A without looping. I can;t see how i can do this with vectorisation or with the built in functions.
My aim is generate a random numbers so 87% of the time I choose A(1), 11% A(2), etc…
Thanks

Best Answer

If you have the Statistics Toolbox installed, there is the built-in RANDSAMPLE command to do this.
A = [0.8756 0.1185 0.0059];
X = randsample(numel(A),5000,true, A); % X gets 5000 samples
tabulate(X)