Mercurial > octave-nkf
annotate scripts/statistics/distributions/unidrnd.m @ 20654:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | d9341b422488 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13171
diff
changeset
|
1 ## Copyright (C) 2012 Rik Wehbring |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19627
diff
changeset
|
2 ## Copyright (C) 2005-2015 John W. Eaton |
6356 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6356 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6356 | 19 |
20 ## -*- texinfo -*- | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
21 ## @deftypefn {Function File} {} unidrnd (@var{n}) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
22 ## @deftypefnx {Function File} {} unidrnd (@var{n}, @var{r}) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
23 ## @deftypefnx {Function File} {} unidrnd (@var{n}, @var{r}, @var{c}, @dots{}) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
24 ## @deftypefnx {Function File} {} unidrnd (@var{n}, [@var{sz}]) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
25 ## Return a matrix of random samples from the discrete uniform distribution |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
26 ## which assumes the integer values 1--@var{n} with equal probability. |
20209
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
27 ## |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
28 ## @var{n} may be a scalar or a multi-dimensional array. |
6356 | 29 ## |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
30 ## When called with a single size argument, return a square matrix with |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
31 ## the dimension specified. When called with more than one scalar argument the |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
32 ## first two arguments are taken as the number of rows and columns and any |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
33 ## further arguments specify additional matrix dimensions. The size may also |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
34 ## be specified with a vector of dimensions @var{sz}. |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
35 ## |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
36 ## If no size arguments are given then the result matrix is the size of |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
37 ## @var{n}. |
6356 | 38 ## @end deftypefn |
39 | |
6396 | 40 ## Author: jwe |
6356 | 41 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
42 function rnd = unidrnd (n, varargin) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
43 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
44 if (nargin < 1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
45 print_usage (); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
46 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
47 |
6396 | 48 if (nargin == 1) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
49 sz = size (n); |
6396 | 50 elseif (nargin == 2) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
51 if (isscalar (varargin{1}) && varargin{1} >= 0) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
52 sz = [varargin{1}, varargin{1}]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
53 elseif (isrow (varargin{1}) && all (varargin{1} >= 0)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
54 sz = varargin{1}; |
6396 | 55 else |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
56 error ("unidrnd: dimension vector must be row vector of non-negative integers"); |
6396 | 57 endif |
58 elseif (nargin > 2) | |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
59 if (any (cellfun (@(x) (! isscalar (x) || x < 0), varargin))) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
60 error ("unidrnd: dimensions must be non-negative integers"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
61 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
62 sz = [varargin{:}]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
63 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
64 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
65 if (! isscalar (n) && ! isequal (size (n), sz)) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
66 error ("unidrnd: N must be scalar or of size SZ"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
67 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
68 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
69 if (iscomplex (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
70 error ("unidrnd: N must not be complex"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
71 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
72 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
73 if (isa (n, "single")) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
74 cls = "single"; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
75 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
76 cls = "double"; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
77 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
78 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
79 if (isscalar (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
80 if (n > 0 && n == fix (n)) |
14683
57569a35765c
Simplify class handling ('single' vs. 'double') for statistical distribution random functions.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
81 rnd = ceil (rand (sz, cls) * n); |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
82 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
83 rnd = NaN (sz, cls); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
84 endif |
6356 | 85 else |
14683
57569a35765c
Simplify class handling ('single' vs. 'double') for statistical distribution random functions.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
86 rnd = ceil (rand (sz, cls) .* n); |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
87 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
88 k = ! (n > 0 & n == fix (n)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
89 rnd(k) = NaN; |
6356 | 90 endif |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
91 |
6356 | 92 endfunction |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
93 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
94 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 %!assert (size (unidrnd (2)), [1, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 %!assert (size (unidrnd (ones (2,1))), [2, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %!assert (size (unidrnd (ones (2,2))), [2, 2]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!assert (size (unidrnd (10, [4 1])), [4, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
99 %!assert (size (unidrnd (10, 4, 1)), [4, 1]) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
100 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
101 ## Test class of input preserved |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %!assert (class (unidrnd (2)), "double") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 %!assert (class (unidrnd (single (2))), "single") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
104 %!assert (class (unidrnd (single ([2 2]))), "single") |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
105 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
106 ## Test input validation |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
107 %!error unidrnd () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
108 %!error unidrnd (10, [1;2;3]) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 %!error unidrnd (10, 2, ones (2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 %!error unidrnd (10*ones (2), 2, 1) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
111 %!error unidrnd (i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
112 |