annotate toolboxes/FullBNT-1.0.7/KPMstats/multirnd.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 r = multirnd(theta,k)
Daniel@0 2 %MULTIRND - Random vector from multinomial distribution.
Daniel@0 3 % r = multirnd(theta,k) returns a vector randomly selected
Daniel@0 4 % from the multinomial distribution with parameter vector
Daniel@0 5 % theta, and count k (i.e. sum(r) = k).
Daniel@0 6 %
Daniel@0 7 % Note: if k is unspecified, then it is assumed k=1.
Daniel@0 8 %
Daniel@0 9 % Author: David Ross
Daniel@0 10 %
Daniel@0 11
Daniel@0 12 %--------------------------------------------------------
Daniel@0 13 % Check the arguments.
Daniel@0 14 %--------------------------------------------------------
Daniel@0 15 error(nargchk(1,2,nargin));
Daniel@0 16
Daniel@0 17 % make sure theta is a vector
Daniel@0 18 if ndims(theta) > 2 | all(size(theta) > 1)
Daniel@0 19 error('theta must be a vector');
Daniel@0 20 end
Daniel@0 21
Daniel@0 22 % if theta is a row vector, convert it to a column vector
Daniel@0 23 if size(theta,1) == 1
Daniel@0 24 theta = theta';
Daniel@0 25 end
Daniel@0 26
Daniel@0 27 % make sure k is a scalar?
Daniel@0 28
Daniel@0 29 % if the number of samples has not been provided, set
Daniel@0 30 % it to one
Daniel@0 31 if nargin == 1
Daniel@0 32 k = 1;
Daniel@0 33 end
Daniel@0 34
Daniel@0 35
Daniel@0 36 %--------------------------------------------------------
Daniel@0 37 % Main...
Daniel@0 38 %--------------------------------------------------------
Daniel@0 39 n = length(theta);
Daniel@0 40 theta_cdf = cumsum(theta);
Daniel@0 41
Daniel@0 42 r = zeros(n,1);
Daniel@0 43 random_vals = rand(k,1);
Daniel@0 44
Daniel@0 45 for j = 1:k
Daniel@0 46 index = min(find(random_vals(j) <= theta_cdf));
Daniel@0 47 r(index) = r(index) + 1;
Daniel@0 48 end