comparison scripts/statistics/distributions/discrete_pdf.m @ 4859:265d566cc770

[project @ 2004-04-08 23:52:45 by jwe]
author jwe
date Thu, 08 Apr 2004 23:52:45 +0000
parents 4e8255035e84
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
4858:499d2ca46982 4859:265d566cc770
31 31
32 if (nargin != 3) 32 if (nargin != 3)
33 usage ("discrete_pdf (x, v, p)"); 33 usage ("discrete_pdf (x, v, p)");
34 endif 34 endif
35 35
36 [r, c] = size (x); 36 sz = size (x);
37 37
38 if (! isvector (v)) 38 if (! isvector (v))
39 error ("discrete_pdf: v must be a vector"); 39 error ("discrete_pdf: v must be a vector");
40 elseif (! isvector (p) || (length (p) != length (v))) 40 elseif (! isvector (p) || (length (p) != length (v)))
41 error ("discrete_pdf: p must be a vector with length (v) elements"); 41 error ("discrete_pdf: p must be a vector with length (v) elements");
42 elseif (! (all (p >= 0) && any (p))) 42 elseif (! (all (p >= 0) && any (p)))
43 error ("discrete_pdf: p must be a nonzero, nonnegative vector"); 43 error ("discrete_pdf: p must be a nonzero, nonnegative vector");
44 endif 44 endif
45 45
46 n = r * c; 46 n = numel (x);
47 m = length (v); 47 m = length (v);
48 x = reshape (x, n, 1); 48 x = reshape (x, n, 1);
49 v = reshape (v, 1, m); 49 v = reshape (v, 1, m);
50 p = reshape (p / sum (p), m, 1); 50 p = reshape (p / sum (p), m, 1);
51 51
52 pdf = zeros (n, 1); 52 pdf = zeros (sz);
53 k = find (isnan (x)); 53 k = find (isnan (x));
54 if (any (k)) 54 if (any (k))
55 pdf (k) = NaN * ones (length (k), 1); 55 pdf (k) = NaN;
56 endif 56 endif
57 k = find (!isnan (x)); 57 k = find (!isnan (x));
58 if (any (k)) 58 if (any (k))
59 n = length (k); 59 n = length (k);
60 pdf (k) = ((x(k) * ones (1, m)) == (ones (n, 1) * v)) * p; 60 pdf (k) = ((x(k) * ones (1, m)) == (ones (n, 1) * v)) * p;
61 endif 61 endif
62 62
63 pdf = reshape (pdf, r, c);
64
65 endfunction 63 endfunction