1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} iqr (@var{x})
21 ## @deftypefnx {Function File} {} iqr (@var{x}, @var{dim})
22 ## Return the interquartile range, i.e., the difference between the upper
23 ## and lower quartile of the input data. If @var{x} is a matrix, do the
24 ## above for first non-singleton dimension of @var{x}.
26 ## If the optional argument @var{dim} is given, operate along this dimension.
28 ## As a measure of dispersion, the interquartile range is less affected by
29 ## outliers than either @code{range} or @code{std}.
30 ## @seealso{range, std}
33 ## Author KH <Kurt.Hornik@wu-wien.ac.at>
34 ## Description: Interquartile range
36 function y = iqr (x, dim)
38 if (nargin != 1 && nargin != 2)
42 if (! (isnumeric (x) || islogical (x)))
43 error ("iqr: X must be a numeric vector or matrix");
50 ## Find the first non-singleton dimension.
51 (dim = find (sz > 1, 1)) || (dim = 1);
53 if (!(isscalar (dim) && dim == fix (dim))
54 || !(1 <= dim && dim <= nd))
55 error ("iqr: DIM must be an integer and a valid dimension");
59 ## This code is a bit heavy, but is needed until empirical_inv
60 ## can take a matrix, rather than just a vector argument.
63 if (isa (x, 'single'))
64 y = zeros (sz, 'single');
68 stride = prod (sz(1:dim-1));
72 while (offset > stride)
76 offset += offset2 * stride * n;
77 rng = [0 : n-1] * stride + offset;
79 y(i) = diff (empirical_inv ([1/4, 3/4], x(rng)));
85 %!assert (iqr (1:101), 50);
86 %!assert (iqr (single(1:101)), single(50));
90 %%! n = iqr (x, 0:10);
91 %%! assert (n, [repmat(100, 1, 10), 1]);
94 %!error iqr (1, 2, 3);
96 %!error iqr (['A'; 'B']);
97 %!error iqr (1:10, 3);