]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/unidrnd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / unidrnd.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 2005-2012 John W. Eaton
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} unidrnd (@var{n})
22 ## @deftypefnx {Function File} {} unidrnd (@var{n}, @var{r})
23 ## @deftypefnx {Function File} {} unidrnd (@var{n}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} unidrnd (@var{n}, [@var{sz}])
25 ## Return a matrix of random samples from the discrete uniform distribution
26 ## which assumes the integer values 1--@var{n} with equal probability.
27 ## @var{n} may be a scalar or a multi-dimensional array.
28 ##
29 ## When called with a single size argument, return a square matrix with
30 ## the dimension specified.  When called with more than one scalar argument the
31 ## first two arguments are taken as the number of rows and columns and any
32 ## further arguments specify additional matrix dimensions.  The size may also
33 ## be specified with a vector of dimensions @var{sz}.
34 ## 
35 ## If no size arguments are given then the result matrix is the size of
36 ## @var{n}.
37 ## @end deftypefn
38
39 ## Author: jwe
40
41 function rnd = unidrnd (n, varargin)
42
43   if (nargin < 1)
44     print_usage ();
45   endif
46
47   if (nargin == 1)
48     sz = size (n);
49   elseif (nargin == 2)
50     if (isscalar (varargin{1}) && varargin{1} >= 0)
51       sz = [varargin{1}, varargin{1}];
52     elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
53       sz = varargin{1};
54     else
55       error ("unidrnd: dimension vector must be row vector of non-negative integers");
56     endif
57   elseif (nargin > 2)
58     if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
59       error ("unidrnd: dimensions must be non-negative integers");
60     endif
61     sz = [varargin{:}];
62   endif
63
64   if (!isscalar (n) && !isequal (size (n), sz))
65     error ("unidrnd: N must be scalar or of size SZ");
66   endif
67
68   if (iscomplex (n))
69     error ("unidrnd: N must not be complex");
70   endif
71
72   if (isa (n, "single"))
73     cls = "single";
74   else
75     cls = "double";
76   endif
77
78   if (isscalar (n))
79     if (n > 0 && n == fix (n))
80       rnd = ceil (rand (sz) * n);
81     else
82       rnd = NaN (sz, cls);
83     endif
84   else
85     rnd = ceil (rand (sz) .* n);
86
87     k = ! (n > 0 & n == fix (n));
88     rnd(k) = NaN;
89   endif
90
91 endfunction
92
93
94 %!assert(size (unidrnd (2)), [1, 1]);
95 %!assert(size (unidrnd (ones(2,1))), [2, 1]);
96 %!assert(size (unidrnd (ones(2,2))), [2, 2]);
97 %!assert(size (unidrnd (10, [4 1])), [4, 1]);
98 %!assert(size (unidrnd (10, 4, 1)), [4, 1]);
99
100 %% Test class of input preserved
101 %!assert(class (unidrnd (2)), "double");
102 %!assert(class (unidrnd (single(2))), "single");
103 %!assert(class (unidrnd (single([2 2]))), "single");
104
105 %% Test input validation
106 %!error unidrnd ()
107 %!error unidrnd (10, [1;2;3])
108 %!error unidrnd (10, 2, ones(2))
109 %!error unidrnd (10*ones(2), 2, 1)
110 %!error unidrnd (i)
111