7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, |
|
2 ## 2006, 2007 Kurt Hornik |
3426
|
3 ## |
3922
|
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. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
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/>. |
3200
|
19 |
3453
|
20 ## -*- texinfo -*- |
4885
|
21 ## @deftypefn {Function File} {} iqr (@var{x}, @var{dim}) |
3453
|
22 ## If @var{x} is a vector, return the interquartile range, i.e., the |
3200
|
23 ## difference between the upper and lower quartile, of the input data. |
|
24 ## |
4885
|
25 ## If @var{x} is a matrix, do the above for first non singleton |
5435
|
26 ## dimension of @var{x}. If the option @var{dim} argument is given, |
4885
|
27 ## then operate along this dimension. |
3453
|
28 ## @end deftypefn |
3426
|
29 |
5428
|
30 ## Author KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
31 ## Description: Interquartile range |
3200
|
32 |
4885
|
33 function y = iqr (x, dim) |
3426
|
34 |
4885
|
35 if (nargin != 1 && nargin != 2) |
6046
|
36 print_usage (); |
3200
|
37 endif |
|
38 |
4885
|
39 nd = ndims (x); |
|
40 sz = size (x); |
|
41 nel = numel (x); |
|
42 if (nargin != 2) |
4886
|
43 ## Find the first non-singleton dimension. |
4885
|
44 dim = 1; |
4886
|
45 while (dim < nd + 1 && sz(dim) == 1) |
4885
|
46 dim = dim + 1; |
|
47 endwhile |
|
48 if (dim > nd) |
|
49 dim = 1; |
|
50 endif |
|
51 else |
4886
|
52 if (! (isscalar (dim) && dim == round (dim)) |
|
53 && dim > 0 |
|
54 && dim < (nd + 1)) |
4885
|
55 error ("iqr: dim must be an integer and valid dimension"); |
|
56 endif |
4681
|
57 endif |
4631
|
58 |
4885
|
59 ## This code is a bit heavy, but is needed until empirical_inv |
|
60 ## takes other than vector arguments. |
4886
|
61 c = sz(dim); |
|
62 sz(dim) = 1; |
4885
|
63 y = zeros (sz); |
4886
|
64 stride = prod (sz(1:dim-1)); |
4885
|
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; |
4631
|
74 |
4885
|
75 y (i) = empirical_inv (3/4, x(rng)) - empirical_inv (1/4, x(rng)); |
4681
|
76 endfor |
3426
|
77 |
3200
|
78 endfunction |