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} {} lognrnd (@var{a}, @var{v}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} lognrnd (@var{a}, @var{v}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} matrix of random samples from the |
|
24 ## lognormal distribution with parameters @var{a} and @var{v}. Both |
|
25 ## @var{a} and @var{v} must be scalar or of size @var{r} by @var{c}. |
|
26 ## Or if @var{sz} is a vector, create a matrix of size @var{sz}. |
|
27 ## |
|
28 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
29 ## the common size of @var{a} and @var{v}. |
|
30 ## @end deftypefn |
|
31 |
|
32 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
33 ## Description: Random deviates from the log normal distribution |
|
34 |
5411
|
35 function rnd = lognrnd (a, v, r, c) |
5410
|
36 |
|
37 if (nargin > 1) |
|
38 if (!isscalar(a) || !isscalar(v)) |
|
39 [retval, a, v] = common_size (a, v); |
|
40 if (retval > 0) |
5411
|
41 error ("lognrnd: a 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 ("lognrnd: r must be a positive integer"); |
5410
|
49 endif |
|
50 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
51 error ("lognrnd: c must be a positive integer"); |
5410
|
52 endif |
|
53 sz = [r, c]; |
|
54 |
|
55 if (any (size (a) != 1) && |
|
56 ((length (size (a)) != length (sz)) || any (size (a) != sz))) |
5411
|
57 error ("lognrnd: a and b must be scalar or of size [r, c]"); |
5410
|
58 endif |
|
59 |
|
60 elseif (nargin == 3) |
|
61 if (isscalar (r) && (r > 0)) |
|
62 sz = [r, r]; |
|
63 elseif (isvector(r) && all (r > 0)) |
|
64 sz = r(:)'; |
|
65 else |
5411
|
66 error ("lognrnd: r must be a postive integer or vector"); |
5410
|
67 endif |
|
68 |
|
69 if (any (size (a) != 1) && |
|
70 ((length (size (a)) != length (sz)) || any (size (a) != sz))) |
5411
|
71 error ("lognrnd: a and b must be scalar or of size sz"); |
5410
|
72 endif |
|
73 elseif (nargin == 2) |
|
74 sz = size(a); |
|
75 else |
5411
|
76 usage ("lognrnd (a, v, r, c)"); |
5410
|
77 endif |
|
78 |
|
79 if (isscalar (a) && isscalar (v)) |
|
80 if (!(a > 0) | !(a < Inf) | !(v > 0) | !(v < Inf)) |
|
81 rnd = NaN * ones (sz); |
|
82 elseif find ((a > 0) & (a < Inf) & (v > 0) & (v < Inf)); |
|
83 rnd = a * exp (sqrt (v) .* randn (sz)); |
|
84 else |
|
85 rnd = zeros (sz); |
|
86 endif |
|
87 else |
|
88 rnd = zeros (sz); |
|
89 k = find (!(a > 0) | !(a < Inf) | !(v > 0) | !(v < Inf)); |
|
90 if (any (k)) |
|
91 rnd(k) = NaN * ones (1, length (k)); |
|
92 endif |
|
93 |
|
94 k = find ((a > 0) & (a < Inf) & (v > 0) & (v < Inf)); |
|
95 if (any (k)) |
|
96 rnd(k) = a(k) .* exp (sqrt (v(k)) .* randn (1, length (k))); |
|
97 endif |
|
98 endif |
|
99 |
|
100 endfunction |