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} {} unifrnd (@var{a}, @var{b}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} unifrnd (@var{a}, @var{b}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} or a @code{size (@var{sz})} matrix of |
|
24 ## random samples from the uniform distribution on [@var{a}, @var{b}]. |
|
25 ## Both @var{a} and @var{b} must be scalar or of size @var{r} by @var{c}. |
|
26 ## |
|
27 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
28 ## the common size of @var{a} and @var{b}. |
|
29 ## @end deftypefn |
|
30 |
5428
|
31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
32 ## Description: Random deviates from the uniform distribution |
|
33 |
5411
|
34 function rnd = unifrnd (a, b, r, c) |
5410
|
35 |
|
36 if (nargin > 1) |
|
37 if (!isscalar(a) || !isscalar(b)) |
|
38 [retval, a, b] = common_size (a, b); |
|
39 if (retval > 0) |
5411
|
40 error ("unifrnd: a and b 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)))) |
5411
|
47 error ("unifrnd: r must be a positive integer"); |
5410
|
48 endif |
|
49 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
50 error ("unifrnd: c must be a positive integer"); |
5410
|
51 endif |
|
52 sz = [r, c]; |
|
53 |
|
54 if (any (size (a) != 1) |
|
55 && (length (size (a)) != length (sz) || any (size (a) != sz))) |
5411
|
56 error ("unifrnd: a and b 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 |
5411
|
64 error ("unifrnd: r must be a postive integer or vector"); |
5410
|
65 endif |
|
66 |
|
67 if (any (size (a) != 1) |
|
68 && (length (size (a)) != length (sz) || any (size (a) != sz))) |
5411
|
69 error ("unifrnd: a and b must be scalar or of size sz"); |
5410
|
70 endif |
|
71 elseif (nargin == 2) |
|
72 sz = size(a); |
|
73 else |
5411
|
74 usage ("unifrnd (a, b, r, c)"); |
5410
|
75 endif |
|
76 |
|
77 if (isscalar(a) && isscalar(b)) |
|
78 if (find (!(-Inf < a) | !(a < b) | !(b < Inf))) |
|
79 rnd = NaN * ones(sz); |
|
80 else |
|
81 rnd = a + (b - a) .* rand (sz); |
|
82 endif |
|
83 else |
|
84 rnd = a + (b - a) .* rand (sz); |
|
85 |
|
86 k = find (!(-Inf < a) | !(a < b) | !(b < Inf)); |
|
87 if (any (k)) |
|
88 rnd(k) = NaN; |
|
89 endif |
|
90 endif |
|
91 |
|
92 endfunction |