Mercurial > hg > octave-nkf
annotate scripts/statistics/base/statistics.m @ 10830:b4ebfd675321
avoid static initialization disaster in dim_vector
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 29 Jul 2010 08:47:26 +0200 |
parents | cab3b148d4e4 |
children | fe3c3dfc07eb |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005, |
8920 | 2 ## 2006, 2007, 2008 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 -*- |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} statistics (@var{x}) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {} statistics (@var{x}, @var{dim}) |
3453 | 23 ## If @var{x} is a matrix, return a matrix with the minimum, first |
24 ## quartile, median, third quartile, maximum, mean, standard deviation, | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
25 ## skewness, and kurtosis of the columns of @var{x} as its columns. |
3200 | 26 ## |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
27 ## If @var{x} is a vector, calculate the statistics along the first |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
28 ## non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
29 ## |
3453 | 30 ## @end deftypefn |
3426 | 31 |
5428 | 32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 33 ## Description: Compute basic statistics |
3200 | 34 |
4885 | 35 function S = statistics (X, dim) |
3426 | 36 |
4885 | 37 if (nargin != 1 && nargin != 2) |
6046 | 38 print_usage (); |
3200 | 39 endif |
40 | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
41 if (!ismatrix(X) || ischar(X)) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
42 error ("statistics: X must be a numeric matrix or vector"); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
43 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
44 |
4885 | 45 nd = ndims (X); |
46 sz = size (X); | |
47 if (nargin != 2) | |
4886 | 48 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
50 if (isempty (dim)) |
4885 | 51 dim = 1; |
52 endif | |
53 else | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
54 if (!(isscalar (dim) && dim == round (dim)) || |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
55 !(1 <= dim && dim <= nd)) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 error ("statistics: DIM must be an integer and a valid dimension"); |
3200 | 57 endif |
4885 | 58 endif |
59 | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
60 if (sz(dim) < 2) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
61 error ("statistics: dimension of X is too small (<2)"); |
4885 | 62 endif |
63 | |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
64 emp_inv = quantile (X, [0.25; 0.5; 0.75], dim, 7); |
4885 | 65 |
66 S = cat (dim, min (X, [], dim), emp_inv, max (X, [], dim), mean (X, dim), | |
10549 | 67 std (X, [], dim), skewness (X, dim), kurtosis (X, dim)); |
3426 | 68 |
3200 | 69 endfunction |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
70 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
71 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
72 %! x = rand(7,5); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
73 %! s = statistics (x); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
74 %! m = median (x); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
75 %! assert (m, s(3,:), eps); |