changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/iqr.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (44 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005,
2## 2006, 2007, 2009 Kurt Hornik
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} {} iqr (@var{x}, @var{dim})
22## If @var{x} is a vector, return the interquartile range, i.e., the
23## difference between the upper and lower quartile, of the input data.
24##
25## If @var{x} is a matrix, do the above for first non-singleton
26## dimension of @var{x}. If the option @var{dim} argument is given,
27## then operate along this dimension.
28## @end deftypefn
29
30## Author KH <Kurt.Hornik@wu-wien.ac.at>
31## Description: Interquartile range
32
33function y = iqr (x, dim)
34
35 if (nargin != 1 && nargin != 2)
36 print_usage ();
37 endif
38
39 nd = ndims (x);
40 sz = size (x);
41 nel = numel (x);
42 if (nargin != 2)
43 ## Find the first non-singleton dimension.
44 dim = 1;
45 while (dim < nd + 1 && sz(dim) == 1)
46 dim = dim + 1;
47 endwhile
48 if (dim > nd)
49 dim = 1;
50 endif
51 else
52 if (! (isscalar (dim) && dim == round (dim))
53 && dim > 0
54 && dim < (nd + 1))
55 error ("iqr: dim must be an integer and valid dimension");
56 endif
57 endif
58
59 ## This code is a bit heavy, but is needed until empirical_inv
60 ## takes other than vector arguments.
61 c = sz(dim);
62 sz(dim) = 1;
63 y = zeros (sz);
64 stride = prod (sz(1:dim-1));
65 for i = 1 : nel / c;
66 offset = i;
67 offset2 = 0;
68 while (offset > stride)
69 offset -= stride;
70 offset2++;
71 endwhile
72 offset += offset2 * stride * c;
73 rng = [0 : c-1] * stride + offset;
74
75 y (i) = empirical_inv (3/4, x(rng)) - empirical_inv (1/4, x(rng));
76 endfor
77
78endfunction