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} {} exprnd (@var{lambda}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} exprnd (@var{lambda}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} matrix of random samples from the |
|
24 ## exponential distribution with parameter @var{lambda}, which must be a |
|
25 ## scalar or of size @var{r} by @var{c}. Or if @var{sz} is a vector, |
|
26 ## 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 size of @var{lambda}. |
|
30 ## @end deftypefn |
|
31 |
5428
|
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
33 ## Description: Random deviates from the exponential distribution |
|
34 |
5411
|
35 function rnd = exprnd (l, r, c) |
5410
|
36 |
|
37 if (nargin == 3) |
|
38 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
39 error ("exprnd: r must be a positive integer"); |
5410
|
40 endif |
|
41 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
42 error ("exprnd: c must be a positive integer"); |
5410
|
43 endif |
|
44 sz = [r, c]; |
|
45 |
|
46 if (any (size (l) != 1) && |
6016
|
47 (length (size (l)) != length (sz) || any (size (l) != sz))) |
5411
|
48 error ("exprnd: lambda must be scalar or of size [r, c]"); |
5410
|
49 endif |
|
50 elseif (nargin == 2) |
|
51 if (isscalar (r) && (r > 0)) |
|
52 sz = [r, r]; |
|
53 elseif (isvector(r) && all (r > 0)) |
|
54 sz = r(:)'; |
|
55 else |
5411
|
56 error ("exprnd: r must be a postive integer or vector"); |
5410
|
57 endif |
|
58 |
|
59 if (any (size (l) != 1) && |
|
60 ((length (size (l)) != length (sz)) || any (size (l) != sz))) |
5411
|
61 error ("exprnd: lambda must be scalar or of size sz"); |
5410
|
62 endif |
|
63 elseif (nargin == 1) |
|
64 sz = size (l); |
|
65 else |
6046
|
66 print_usage (); |
5410
|
67 endif |
|
68 |
|
69 |
|
70 if (isscalar (l)) |
|
71 if ((l > 0) && (l < Inf)) |
6339
|
72 rnd = rande(sz) / l; |
5410
|
73 else |
|
74 rnd = NaN * ones (sz); |
|
75 endif |
|
76 else |
|
77 rnd = zeros (sz); |
|
78 k = find (!(l > 0) | !(l < Inf)); |
|
79 if (any (k)) |
|
80 rnd(k) = NaN; |
|
81 endif |
|
82 k = find ((l > 0) & (l < Inf)); |
|
83 if (any (k)) |
6339
|
84 rnd(k) = rande(size(k)) / l(k); |
5410
|
85 endif |
|
86 endif |
|
87 |
|
88 endfunction |