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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3191
|
19 |
3456
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} laplace_rnd (@var{r}, @var{c}) |
4859
|
22 ## @deftypefnx {Function File} {} laplace_rnd (@var{sz}); |
3456
|
23 ## Return an @var{r} by @var{c} matrix of random numbers from the |
5457
|
24 ## Laplace distribution. Or if @var{sz} is a vector, create a matrix of |
4859
|
25 ## @var{sz}. |
3456
|
26 ## @end deftypefn |
3426
|
27 |
5428
|
28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
29 ## Description: Random deviates from the Laplace distribution |
3191
|
30 |
|
31 function rnd = laplace_rnd (r, c) |
3426
|
32 |
4859
|
33 if (nargin == 2) |
|
34 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
|
35 error ("laplace_rnd: r must be a positive integer"); |
|
36 endif |
|
37 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
|
38 error ("laplace_rnd: c must be a positive integer"); |
|
39 endif |
|
40 sz = [r, c]; |
|
41 elseif (nargin == 1) |
|
42 if (isscalar (r) && (r > 0)) |
|
43 sz = [r, r]; |
|
44 elseif (isvector(r) && all (r > 0)) |
|
45 sz = r(:)'; |
|
46 else |
|
47 error ("laplace_rnd: r must be a postive integer or vector"); |
|
48 endif |
|
49 else |
6046
|
50 print_usage (); |
3191
|
51 endif |
|
52 |
4859
|
53 tmp = rand (sz); |
3426
|
54 rnd = ((tmp < 1/2) .* log (2 * tmp) |
|
55 - (tmp > 1/2) .* log (2 * (1 - tmp))); |
|
56 |
3191
|
57 endfunction |