comparison toolboxes/FullBNT-1.0.7/KPMstats/sample.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function x = sample(p, n)
2 % SAMPLE Sample from categorical distribution.
3 % Returns a row vector of integers, sampled according to the probability
4 % distribution p.
5 % Uses the stick-breaking algorithm.
6 % Much faster algorithms are also possible.
7
8 if nargin < 2
9 n = 1;
10 end
11
12 cdf = cumsum(p(:));
13 for i = 1:n
14 x(i) = sum(cdf < rand) + 1;
15 end