5410
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5411
|
21 ## @deftypefn {Function File} {} normrnd (@var{m}, @var{v}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} normrnd (@var{m}, @var{v}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} or @code{size (@var{sz})} matrix of |
|
24 ## random samples from the normal distribution with parameters @var{m} |
|
25 ## and @var{v}. Both @var{m} and @var{v} must be scalar or of size |
|
26 ## @var{r} by @var{c}. |
|
27 ## |
|
28 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
29 ## the common size of @var{m} and @var{v}. |
|
30 ## @end deftypefn |
|
31 |
5428
|
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
33 ## Description: Random deviates from the normal distribution |
|
34 |
5411
|
35 function rnd = normrnd (m, v, r, c) |
5410
|
36 |
|
37 if (nargin > 1) |
|
38 if (!isscalar(m) || !isscalar(v)) |
|
39 [retval, m, v] = common_size (m, v); |
|
40 if (retval > 0) |
5411
|
41 error ("normrnd: m and v must be of common size or scalar"); |
5410
|
42 endif |
|
43 endif |
|
44 endif |
|
45 |
|
46 if (nargin == 4) |
|
47 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
48 error ("normrnd: r must be a positive integer"); |
5410
|
49 endif |
|
50 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
51 error ("normrnd: c must be a positive integer"); |
5410
|
52 endif |
|
53 sz = [r, c]; |
|
54 |
|
55 if (any (size (m) != 1) |
|
56 && (length (size (m)) != length (sz) || any (size (m) != sz))) |
5411
|
57 error ("normrnd: m and v must be scalar or of size [r, c]"); |
5410
|
58 endif |
|
59 elseif (nargin == 3) |
|
60 if (isscalar (r) && (r > 0)) |
|
61 sz = [r, r]; |
|
62 elseif (isvector(r) && all (r > 0)) |
|
63 sz = r(:)'; |
|
64 else |
5411
|
65 error ("normrnd: r must be a postive integer or vector"); |
5410
|
66 endif |
|
67 |
|
68 if (any (size (m) != 1) |
|
69 && (length (size (m)) != length (sz) || any (size (m) != sz))) |
5411
|
70 error ("normrnd: m and v must be scalar or of size sz"); |
5410
|
71 endif |
|
72 elseif (nargin == 2) |
|
73 sz = size(m); |
|
74 else |
5411
|
75 usage ("normrnd (m, v, r, c)"); |
5410
|
76 endif |
|
77 |
|
78 if (isscalar (m) && isscalar (v)) |
|
79 if (find (isnan (m) | isinf (m) | !(v > 0) | !(v < Inf))) |
|
80 rnd = NaN * ones (sz); |
|
81 else |
|
82 rnd = m + sqrt (v) .* randn (sz); |
|
83 endif |
|
84 else |
|
85 rnd = m + sqrt (v) .* randn (sz); |
|
86 k = find (isnan (m) | isinf (m) | !(v > 0) | !(v < Inf)); |
|
87 if (any (k)) |
|
88 rnd(k) = NaN; |
|
89 endif |
|
90 endif |
|
91 |
|
92 endfunction |