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} {} statistics (@var{x})
21 ## @deftypefnx {Function File} {} statistics (@var{x}, @var{dim})
22 ## Return a vector with the minimum, first quartile, median, third quartile,
23 ## maximum, mean, standard deviation, skewness, and kurtosis of the elements of
24 ## the vector @var{x}.
26 ## If @var{x} is a matrix, calculate statistics over the first
27 ## non-singleton dimension.
28 ## If the optional argument @var{dim} is given, operate along this dimension.
29 ## @seealso{min, max, median, mean, std, skewness, kurtosis}
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
33 ## Description: Compute basic statistics
35 function stats = statistics (x, dim)
37 if (nargin != 1 && nargin != 2)
41 if (! (isnumeric (x) || islogical (x)))
42 error ("statistics: X must be a numeric vector or matrix");
48 ## Find the first non-singleton dimension.
49 (dim = find (sz > 1, 1)) || (dim = 1);
51 if (!(isscalar (dim) && dim == fix (dim))
52 || !(1 <= dim && dim <= nd))
53 error ("statistics: DIM must be an integer and a valid dimension");
58 error ("statistics: dimension of X is too small (<2)");
61 emp_inv = quantile (x, [0.25; 0.5; 0.75], dim, 7);
63 stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim),
64 std (x, [], dim), skewness (x, dim), kurtosis (x, dim));
71 %! s = statistics (x);
72 %! assert (min (x), s(1,:), eps);
73 %! assert (median (x), s(3,:), eps);
74 %! assert (max (x), s(5,:), eps);
75 %! assert (mean (x), s(6,:), eps);
76 %! assert (std (x), s(7,:), eps);
77 %! assert (skewness (x), s(8,:), eps);
78 %! assert (kurtosis (x), s(9,:), eps);
80 %% Test input validation
82 %!error statistics (1, 2, 3)
83 %!error statistics (['A'; 'B'])
84 %!error statistics (1, ones(2,2))
85 %!error statistics (1, 1.5)
86 %!error statistics (1, 0)
87 %!error statistics (1, 3)
88 %!error statistics (1)