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