1########################################################################
2##
3## Copyright (C) 1995-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} range (@var{x})
28## @deftypefnx {} {} range (@var{x}, @var{dim})
29## Return the range, i.e., the difference between the maximum and the minimum
30## of the input data.
31##
32## If @var{x} is a vector, the range is calculated over the elements of
33## @var{x}.  If @var{x} is a matrix, the range is calculated over each column
34## of @var{x}.
35##
36## If the optional argument @var{dim} is given, operate along this dimension.
37##
38## The range is a quickly computed measure of the dispersion of a data set, but
39## is less accurate than @code{iqr} if there are outlying data points.
40## @seealso{bounds, iqr, mad, std}
41## @end deftypefn
42
43function y = range (x, dim)
44
45  if (nargin < 1 || nargin > 2)
46    print_usage ();
47  endif
48
49  if (nargin == 1)
50    y = max (x) - min (x);
51  else
52    y = max (x, [], dim) - min (x, [], dim);
53  endif
54
55endfunction
56
57
58%!assert (range (1:10), 9)
59%!assert (range (single (1:10)), single (9))
60%!assert (range (magic (3)), [5, 8, 5])
61%!assert (range (magic (3), 2), [7; 4; 7])
62%!assert (range (2), 0)
63
64## Test input validation
65%!error range ()
66%!error range (1, 2, 3)
67