Mercurial > hg > camir-aes2014
annotate 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 |
rev | line source |
---|---|
wolffd@0 | 1 function x = sample(p, n) |
wolffd@0 | 2 % SAMPLE Sample from categorical distribution. |
wolffd@0 | 3 % Returns a row vector of integers, sampled according to the probability |
wolffd@0 | 4 % distribution p. |
wolffd@0 | 5 % Uses the stick-breaking algorithm. |
wolffd@0 | 6 % Much faster algorithms are also possible. |
wolffd@0 | 7 |
wolffd@0 | 8 if nargin < 2 |
wolffd@0 | 9 n = 1; |
wolffd@0 | 10 end |
wolffd@0 | 11 |
wolffd@0 | 12 cdf = cumsum(p(:)); |
wolffd@0 | 13 for i = 1:n |
wolffd@0 | 14 x(i) = sum(cdf < rand) + 1; |
wolffd@0 | 15 end |