function r=discrnd(M,n) %function r=discrnd(bins,n) % sample n RVs from a discrete probability concentrated on [1..N] where % the probability mass is proportional to the positive vector bins % % Guy Lebanon, 2004 cM=cumsum(M); rr=unifrnd(0,cM(end),[1 n]); for i=1:n, [tmp,ind] = max(cM >= rr(i)); r(i) = ind; end