5475
|
1 ## Copyright (C) 2005 John W. Eaton |
|
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 -*- |
|
21 ## @deftypefn {Function File} {} unidrnd (@var{mx}); |
|
22 ## @deftypefn {Function File} {} unidrnd (@var{mx}, @var{v}); |
|
23 ## @deftypefn {Function File} {} unidrnd (@var{mx}, @var{m}, @var{n}, @dots{}); |
|
24 ## Return random values from discrete uniform distribution, with maximum |
|
25 ## value(s) given by the integer @var{mx}, which may be a scalar or |
|
26 ## multidimensional array. |
|
27 ## |
|
28 ## If @var{mx} is a scalar, the size of the result is specified by |
|
29 ## the vector @var{v}, or by the optional arguments @var{m}, @var{n}, |
|
30 ## @dots{}. Otherwise, the size of the result is the same as the size |
|
31 ## of @var{mx}. |
|
32 ## @end deftypefn |
|
33 |
|
34 ## Author: jwe |
|
35 |
|
36 function retval = unidrnd (n, varargin) |
|
37 if (nargin == 1) |
|
38 dims = size (n); |
|
39 elseif (nargin == 2) |
5503
|
40 if (rows (varargin{1}) == 1 && columns (varargin{1}) > 1) |
5475
|
41 dims = varargin{1}; |
|
42 else |
|
43 error ("unidrnd: invalid dimension vector"); |
|
44 endif |
|
45 elseif (nargin > 2) |
|
46 for i = 1:nargin-1 |
|
47 if (! isscalar (varargin{i})) |
|
48 error ("unidrnd: expecting scalar dimensions"); |
|
49 endif |
|
50 endfor |
|
51 dims = [varargin{:}]; |
|
52 else |
|
53 error ("unidrnd (n, ...)"); |
|
54 endif |
|
55 if (isscalar (n) |
|
56 || (length (size (n)) == length (dims) && all (size (n) == dims))) |
|
57 retval = ceil (rand (dims) .* n); |
|
58 else |
|
59 error ("unidrnd: dimension mismatch"); |
|
60 endif |
|
61 endfunction |