3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
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/>. |
3191
|
18 |
3456
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} laplace_rnd (@var{r}, @var{c}) |
4859
|
21 ## @deftypefnx {Function File} {} laplace_rnd (@var{sz}); |
3456
|
22 ## Return an @var{r} by @var{c} matrix of random numbers from the |
5457
|
23 ## Laplace distribution. Or if @var{sz} is a vector, create a matrix of |
4859
|
24 ## @var{sz}. |
3456
|
25 ## @end deftypefn |
3426
|
26 |
5428
|
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
28 ## Description: Random deviates from the Laplace distribution |
3191
|
29 |
|
30 function rnd = laplace_rnd (r, c) |
3426
|
31 |
4859
|
32 if (nargin == 2) |
|
33 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
|
34 error ("laplace_rnd: r must be a positive integer"); |
|
35 endif |
|
36 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
|
37 error ("laplace_rnd: c must be a positive integer"); |
|
38 endif |
|
39 sz = [r, c]; |
|
40 elseif (nargin == 1) |
|
41 if (isscalar (r) && (r > 0)) |
|
42 sz = [r, r]; |
|
43 elseif (isvector(r) && all (r > 0)) |
|
44 sz = r(:)'; |
|
45 else |
7007
|
46 error ("laplace_rnd: r must be a positive integer or vector"); |
4859
|
47 endif |
|
48 else |
6046
|
49 print_usage (); |
3191
|
50 endif |
|
51 |
4859
|
52 tmp = rand (sz); |
3426
|
53 rnd = ((tmp < 1/2) .* log (2 * tmp) |
|
54 - (tmp > 1/2) .* log (2 * (1 - tmp))); |
|
55 |
3191
|
56 endfunction |