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

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cc4b1211e677
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