7017
|
1 ## Copyright (C) 2005, 2007 John W. Eaton |
6356
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6356
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6356
|
18 |
|
19 ## -*- texinfo -*- |
6396
|
20 ## @deftypefn {Function File} {} unidrnd (@var{mx}); |
|
21 ## @deftypefnx {Function File} {} unidrnd (@var{mx}, @var{v}); |
|
22 ## @deftypefnx {Function File} {} unidrnd (@var{mx}, @var{m}, @var{n}, @dots{}); |
|
23 ## Return random values from discrete uniform distribution, with maximum |
|
24 ## value(s) given by the integer @var{mx}, which may be a scalar or |
|
25 ## multidimensional array. |
6356
|
26 ## |
6396
|
27 ## If @var{mx} is a scalar, the size of the result is specified by |
|
28 ## the vector @var{v}, or by the optional arguments @var{m}, @var{n}, |
|
29 ## @dots{}. Otherwise, the size of the result is the same as the size |
|
30 ## of @var{mx}. |
6356
|
31 ## @end deftypefn |
|
32 |
6396
|
33 ## Author: jwe |
6356
|
34 |
6396
|
35 function retval = unidrnd (n, varargin) |
|
36 if (nargin == 1) |
|
37 dims = size (n); |
|
38 elseif (nargin == 2) |
|
39 if (rows (varargin{1}) == 1 && columns (varargin{1}) > 1) |
|
40 dims = varargin{1}; |
|
41 else |
|
42 error ("unidrnd: invalid dimension vector"); |
|
43 endif |
|
44 elseif (nargin > 2) |
|
45 for i = 1:nargin-1 |
|
46 if (! isscalar (varargin{i})) |
|
47 error ("unidrnd: expecting scalar dimensions"); |
|
48 endif |
|
49 endfor |
|
50 dims = [varargin{:}]; |
6356
|
51 else |
6396
|
52 error ("unidrnd (n, ...)"); |
6356
|
53 endif |
6396
|
54 if (isscalar (n) |
|
55 || (length (size (n)) == length (dims) && all (size (n) == dims))) |
|
56 retval = ceil (rand (dims) .* n); |
|
57 else |
|
58 error ("unidrnd: dimension mismatch"); |
|
59 endif |
6356
|
60 endfunction |