thanks in advance
christoph
Without thinking through the details, it seems like you could use hist or
histc to select what to return after letting X, the bin boundaries be
defined by cumsum(p/sum(p)) and feeding it uniform variates generated by
rand.
Christoph, if you have the Statistics Toolbox, try "help randsample" and
look at the example at the end. This example returns random values from the
letters 'ACGT', but you could choose instead to return random values from
1:4.
-- Tom
So the usual way to do this is to construct a mapping function from
a uniform rv to the rv with the distribution you want. This mapping
function is the inverse of the CDF of the distribution.
The CDF for discrete rvs is the cumsum of the PDF:
my_cdf = cumsum(p)
% my_cdf = [2 6 7 10 12]
Now if you take a bunch of uniform samples from [0 to 12), and take
the index of the next greatest element of my_cdf, you'll have your
distribution. You could do it manually (using find and a loop), but a
look-up table is better:
clear q;
q(my_cdf) = 1;
my_lut = cumsum([1 q]);
Now samples may be generated using:
drawn_samples = my_lut(floor(my_cdf(end)*rand(1,N)) + 1);
--
Peter Boettcher <boet...@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/
A fast one line way :
[toto , draw_samples] = histc(rand(1 , N), [0 cumsum(p)./sum(p)])
Sébastien