FireStats is not installed in the database
Laryngitis homøopatiske midler køb propecia edderkop bid retsmidler

Matlab ‘sample’ function

Author:nick @ January 30th, 2008 Leave a Comment

I recently discovered Matlab’s “sample” function, which I have implemented on my own several times before. There are always slightly annoying implementation details to work out, and it’s very nice to have a function that is standard to do it for me.

The idea is to sample from a multinomial distribution, which is something you need to do from time to time for various reasons. Here is an example of usage:

>> sample([.25, .5, .1, .15],1),  ans =  3
>> sample([.25, .5, .1, .15],1),  ans =  4
>> sample([.25, .5, .1, .15],1),  ans =  1
>> sample([.25, .5, .1, .15],1),  ans =  4
>> sample([.25, .5, .1, .15],1),  ans =  2
>> sample([.25, .5, .1, .15],1),  ans =  1
>> sample([.25, .5, .1, .15],1),  ans =  2
>> sample([.25, .5, .1, .15],1),  ans =  2

a = sample([.25, .5, .1, .15],10000);
>> nnz(a == 1),  ans =  2483
>> nnz(a == 2),  ans =  5027
>> nnz(a == 3),  ans =  988
>> nnz(a == 4),  ans =  1502