]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/range.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / range.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ## Copyright (C) 2009 Jaroslav Hajek
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} {} range (@var{x})
22 ## @deftypefnx {Function File} {} range (@var{x}, @var{dim})
23 ## Return the range, i.e., the difference between the maximum and the minimum
24 ## of the input data.  If @var{x} is a vector, the range is calculated over
25 ## the elements of @var{x}.  If @var{x} is a matrix, the range is calculated
26 ## over each column of @var{x}.
27 ##
28 ## If the optional argument @var{dim} is given, operate along this dimension.
29 ##
30 ## The range is a quickly computed measure of the dispersion of a data set, but
31 ## is less accurate than @code{iqr} if there are outlying data points.
32 ## @seealso{iqr, std}
33 ## @end deftypefn
34
35 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
36 ## Description: Compute range
37
38 function y = range (x, dim)
39
40   if (nargin < 1 || nargin > 2)
41     print_usage ();
42   endif
43
44   if (nargin == 1)
45     y = max (x) - min (x);
46   else
47     y = max (x, [], dim) - min (x, [], dim);
48   endif
49
50 endfunction
51
52
53 %!assert(range (1:10), 9);
54 %!assert(range (single(1:10)), single(9));
55 %!assert(range (magic (3)), [5, 8, 5]);
56 %!assert(range (magic (3), 2), [7; 4; 7]);
57 %!assert(range (2), 0);
58
59 %% Test input validation
60 %!error range ()
61 %!error range (1, 2, 3)