comparison toolboxes/FullBNT-1.0.7/KPMstats/multirnd.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function r = multirnd(theta,k)
2 %MULTIRND - Random vector from multinomial distribution.
3 % r = multirnd(theta,k) returns a vector randomly selected
4 % from the multinomial distribution with parameter vector
5 % theta, and count k (i.e. sum(r) = k).
6 %
7 % Note: if k is unspecified, then it is assumed k=1.
8 %
9 % Author: David Ross
10 %
11
12 %--------------------------------------------------------
13 % Check the arguments.
14 %--------------------------------------------------------
15 error(nargchk(1,2,nargin));
16
17 % make sure theta is a vector
18 if ndims(theta) > 2 | all(size(theta) > 1)
19 error('theta must be a vector');
20 end
21
22 % if theta is a row vector, convert it to a column vector
23 if size(theta,1) == 1
24 theta = theta';
25 end
26
27 % make sure k is a scalar?
28
29 % if the number of samples has not been provided, set
30 % it to one
31 if nargin == 1
32 k = 1;
33 end
34
35
36 %--------------------------------------------------------
37 % Main...
38 %--------------------------------------------------------
39 n = length(theta);
40 theta_cdf = cumsum(theta);
41
42 r = zeros(n,1);
43 random_vals = rand(k,1);
44
45 for j = 1:k
46 index = min(find(random_vals(j) <= theta_cdf));
47 r(index) = r(index) + 1;
48 end