1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 2007-2012 David Bateman
4 ## This file is part of Octave.
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.
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.
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/>.
21 ## @deftypefn {Function File} {} unidinv (@var{x}, @var{n})
22 ## For each element of @var{x}, compute the quantile (the inverse of
23 ## the CDF) at @var{x} of the discrete uniform distribution which assumes
24 ## the integer values 1--@var{n} with equal probability.
27 function inv = unidinv (x, n)
34 [retval, x, n] = common_size (x, n);
36 error ("unidcdf: X and N must be of common size or scalars");
40 if (iscomplex (x) || iscomplex (n))
41 error ("unidinv: X and N must not be complex");
44 if (isa (x, "single") || isa (n, "single"))
45 inv = NaN (size (x), "single");
50 ## For Matlab compatibility, unidinv(0) = NaN
51 k = (x > 0) & (x <= 1) & (n > 0 & n == fix (n));
53 inv(k) = floor (x(k) * n);
55 inv(k) = floor (x(k) .* n(k));
62 %! x = [-1 0 0.5 1 2];
63 %!assert(unidinv (x, 10*ones(1,5)), [NaN NaN 5 10 NaN], eps);
64 %!assert(unidinv (x, 10), [NaN NaN 5 10 NaN], eps);
65 %!assert(unidinv (x, 10*[0 1 NaN 1 1]), [NaN NaN NaN 10 NaN], eps);
66 %!assert(unidinv ([x(1:2) NaN x(4:5)], 10), [NaN NaN NaN 10 NaN], eps);
68 %% Test class of input preserved
69 %!assert(unidinv ([x, NaN], 10), [NaN NaN 5 10 NaN NaN], eps);
70 %!assert(unidinv (single([x, NaN]), 10), single([NaN NaN 5 10 NaN NaN]), eps);
71 %!assert(unidinv ([x, NaN], single(10)), single([NaN NaN 5 10 NaN NaN]), eps);
73 %% Test input validation
76 %!error unidinv (1,2,3)
77 %!error unidinv (ones(3),ones(2))
78 %!error unidinv (ones(2),ones(3))
79 %!error unidinv (i, 2)
80 %!error unidinv (2, i)