Mercurial > hg > octave-lyh
annotate scripts/statistics/distributions/normrnd.m @ 12749:e7cc2d4a6db3 stable
Fix range of sigma in normal distribution to exclude 0.
* normcdf.m, normpdf.m: Correct 's >= 0' to 's > 0'.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 16 Jun 2011 21:05:10 -0700 |
parents | c792872f8942 |
children | 19b9f17d22af |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 Kurt Hornik |
5410 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5410 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5410 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} normrnd (@var{m}, @var{s}, @var{r}, @var{c}) |
6617 | 21 ## @deftypefnx {Function File} {} normrnd (@var{m}, @var{s}, @var{sz}) |
5410 | 22 ## Return an @var{r} by @var{c} or @code{size (@var{sz})} matrix of |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
23 ## random samples from the normal distribution with parameters mean @var{m} |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
24 ## and standard deviation @var{s}. Both @var{m} and @var{s} must be scalar |
6617 | 25 ## or of size @var{r} by @var{c}. |
5410 | 26 ## |
27 ## If @var{r} and @var{c} are omitted, the size of the result matrix is | |
6617 | 28 ## the common size of @var{m} and @var{s}. |
5410 | 29 ## @end deftypefn |
30 | |
5428 | 31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 32 ## Description: Random deviates from the normal distribution |
33 | |
6617 | 34 function rnd = normrnd (m, s, r, c) |
5410 | 35 |
36 if (nargin > 1) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 if (!isscalar (m) || !isscalar (s)) |
6617 | 38 [retval, m, s] = common_size (m, s); |
5410 | 39 if (retval > 0) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
40 error ("normrnd: M and S must be of common size or scalar"); |
5410 | 41 endif |
42 endif | |
43 endif | |
44 | |
45 if (nargin == 4) | |
46 if (! (isscalar (r) && (r > 0) && (r == round (r)))) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
47 error ("normrnd: R must be a positive integer"); |
5410 | 48 endif |
49 if (! (isscalar (c) && (c > 0) && (c == round (c)))) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
50 error ("normrnd: C must be a positive integer"); |
5410 | 51 endif |
52 sz = [r, c]; | |
53 | |
54 if (any (size (m) != 1) | |
10549 | 55 && (length (size (m)) != length (sz) || any (size (m) != sz))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
56 error ("normrnd: M and S must be scalar or of size [R, C]"); |
5410 | 57 endif |
58 elseif (nargin == 3) | |
59 if (isscalar (r) && (r > 0)) | |
60 sz = [r, r]; | |
61 elseif (isvector(r) && all (r > 0)) | |
62 sz = r(:)'; | |
63 else | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
64 error ("normrnd: R must be a positive integer or vector"); |
5410 | 65 endif |
66 | |
67 if (any (size (m) != 1) | |
10549 | 68 && (length (size (m)) != length (sz) || any (size (m) != sz))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
69 error ("normrnd: M and S must be scalar or of size SZ"); |
5410 | 70 endif |
71 elseif (nargin == 2) | |
72 sz = size(m); | |
73 else | |
6046 | 74 print_usage (); |
5410 | 75 endif |
76 | |
6617 | 77 if (isscalar (m) && isscalar (s)) |
78 if (find (isnan (m) | isinf (m) | !(s > 0) | !(s < Inf))) | |
10525
3306cfcb856e
Replace constructs like "NaN * one()" with "NaN()" and "Inf * ones ()" with "Inf()"
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
79 rnd = NaN (sz); |
5410 | 80 else |
6617 | 81 rnd = m + s .* randn (sz); |
5410 | 82 endif |
83 else | |
6617 | 84 rnd = m + s .* randn (sz); |
85 k = find (isnan (m) | isinf (m) | !(s > 0) | !(s < Inf)); | |
5410 | 86 if (any (k)) |
87 rnd(k) = NaN; | |
88 endif | |
89 endif | |
90 | |
91 endfunction |