Mercurial > octave-nkf
annotate scripts/statistics/distributions/discrete_rnd.m @ 10549:95c3e38098bf
Untabify .m scripts
author | Rik <code@nomad.inbox5.com> |
---|---|
date | Fri, 23 Apr 2010 11:28:50 -0700 |
parents | 16f53d29049f |
children | be55736a0783 |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, 2007, 2009 |
7017 | 2 ## Kurt Hornik |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
3191 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
3191 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3191 | 19 |
3456 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} discrete_rnd (@var{n}, @var{v}, @var{p}) | |
4859 | 22 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, @var{r}, @var{c}) |
23 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, @var{sz}) | |
3456 | 24 ## Generate a row vector containing a random sample of size @var{n} from |
25 ## the univariate distribution which assumes the values in @var{v} with | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
26 ## probabilities @var{p}. @var{n} must be a scalar. |
3191 | 27 ## |
4859 | 28 ## If @var{r} and @var{c} are given create a matrix with @var{r} rows and |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
29 ## @var{c} columns. Or if @var{sz} is a vector, create a matrix of size |
4859 | 30 ## @var{sz}. |
3456 | 31 ## @end deftypefn |
3191 | 32 |
5428 | 33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 34 ## Description: Random deviates from a discrete distribution |
3191 | 35 |
4859 | 36 function rnd = discrete_rnd (v, p, r, c) |
37 | |
38 if (nargin == 4) | |
39 if (! (isscalar (r) && (r > 0) && (r == round (r)))) | |
40 error ("discrete_rnd: r must be a positive integer"); | |
41 endif | |
42 if (! (isscalar (c) && (c > 0) && (c == round (c)))) | |
43 error ("discrete_rnd: c must be a positive integer"); | |
44 endif | |
45 sz = [r, c]; | |
46 elseif (nargin == 3) | |
47 ## A potential problem happens here if all args are scalar, as | |
48 ## we can't distiguish between the command syntax. Thankfully this | |
49 ## case doesn't make much sense. So we assume the first syntax | |
50 ## if the first arg is scalar | |
3426 | 51 |
4859 | 52 if (isscalar (v)) |
53 sz = [1, floor(v)]; | |
54 v = p; | |
55 p = r; | |
56 else | |
57 if (isscalar (r) && (r > 0)) | |
10549 | 58 sz = [r, r]; |
4859 | 59 elseif (isvector(r) && all (r > 0)) |
10549 | 60 sz = r(:)'; |
4859 | 61 else |
10549 | 62 error ("discrete_rnd: r must be a positive integer or vector"); |
4859 | 63 endif |
64 endif | |
65 else | |
6046 | 66 print_usage (); |
3191 | 67 endif |
68 | |
4030 | 69 if (! isvector (v)) |
3456 | 70 error ("discrete_rnd: v must be a vector"); |
4030 | 71 elseif (! isvector (p) || (length (p) != length (v))) |
3456 | 72 error ("discrete_rnd: p must be a vector with length (v) elements"); |
73 elseif (! (all (p >= 0) && any (p))) | |
74 error ("discrete_rnd: p must be a nonzero, nonnegative vector"); | |
3191 | 75 endif |
76 | |
6350 | 77 rnd = v (lookup (cumsum (p (1 : end-1)) / sum(p), rand (sz)) + 1); |
3191 | 78 endfunction |