File: range.m

package info (click to toggle)
octave 11.0.92-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,624 kB
  • sloc: cpp: 347,499; ansic: 85,112; fortran: 20,693; objc: 10,276; sh: 8,747; lex: 4,496; yacc: 4,406; perl: 1,544; java: 1,365; awk: 1,282; makefile: 666; xml: 192
file content (153 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
########################################################################
##
## Copyright (C) 1995-2026 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {@var{y} =} range (@var{x})
## @deftypefnx {} {@var{y} =} range (@var{x}, @var{dim})
## @deftypefnx {} {@var{y} =} range (@var{x}, @var{vecdim})
## @deftypefnx {} {@var{y} =} range (@var{x}, "all")
## @deftypefnx {} {@var{y} =} range (@dots{}, @var{nanflag})
## Return the difference between the maximum and the minimum values of the
## input data @var{x}.
##
## If @var{x} is a vector, then @code{range (@var{x})} returns the difference
## between the maximum and minimum values of the elements in @var{x}.
##
## If @var{x} is a matrix, then @code{range (@var{x})} returns a row vector
## @var{y} with the difference between the maximum and minimum values for each
## column of @var{x}.
##
## If @var{x} is an array, then @code{range (@var{x})} computes the difference
## between the maximum and minimum values along the first non-singleton
## dimension of @var{x}.
##
## The data in @var{x} must be numeric.  By default, any NaN values are
## ignored.  The size of @var{r} is equal to the size of @var{x} except for
## the operating dimension, which becomes 1.
##
## The optional input @var{dim} specifies the dimension to operate on and must
## be a positive integer.  Specifying any singleton dimension of @var{x},
## including any dimension exceeding @code{ndims (@var{x})}, will return
## @code{@var{x}}.
##
## Specifying multiple dimensions with input @var{vecdim}, a vector of
## non-repeating dimensions, will operate along the array slice defined by
## @var{vecdim}.  If @var{vecdim} indexes all dimensions of @var{x}, then it is
## equivalent to the option @qcode{"all"}.  Any dimension in @var{vecdim}
## greater than @code{ndims (@var{x})} is ignored.
##
## Specifying the dimension as @qcode{"all"} will cause @code{range} to operate
## on all elements of @var{x}, and is equivalent to @code{range (@var{x}(:))}.
##
## The optional variable @var{nanflag} specifies whether to include or exclude
## NaN values from the calculation using any of the previously specified input
## argument combinations.  The default value for @var{nanflag} is
## @qcode{"omitnan"} which does not include NaN values in the result.  If the
## argument @qcode{"includenan"} is given, and there is a NaN present, then the
## corresponding result will be NaN.
##
## Usage Note: The range is a quickly computed measure of the dispersion of a
## data set, but is less accurate than @code{iqr} if there are outlying data
## points.
## @seealso{bounds, iqr, mad, std}
## @end deftypefn

function y = range (x, varargin)

  if (nargin < 1 || nargin > 3)
    print_usage ();
  endif

  if (! (isnumeric (x) || islogical (x)))
    error ("range: X must be a numeric array");
  endif

  if (isempty (varargin))
    y = max (x) - min (x);
  else
    y = max (x, [], varargin{:}) - min (x, [], varargin{:});
  endif

endfunction


%!assert (range (1:10), 9)
%!assert (range (single (1:10)), single (9))
%!assert (range (magic (3)), [5, 8, 5])
%!assert (range (magic (3), 2), [7; 4; 7])
%!assert (range (2), 0)
%!test
%! x = magic (3);
%! x(2,3) = NaN;
%! assert (range (x), [5, 8, 4]);
%! assert (range (x, "omitnan"), [5, 8, 4]);
%! assert (range (x, "includenan"), [5, 8, NaN]);
%! assert (range (x, 2), [7; 2; 7]);
%! assert (range (x, 2, "omitnan"), [7; 2; 7]);
%! assert (range (x, 2, "includenan"), [7; NaN; 7]);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! assert (range (x, 3), x(:,:,3) - x(:,:,1));
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! assert (range (x, [2, 3]), x(:,3,3) - x(:,1,1));
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! assert (range (x, [1, 3]), x(3,:,3) - x(1,:,1));
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! assert (range (x, [1, 2, 3]), 26);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! assert (range (x, "all"), 26);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! x(3) = NaN;
%! assert (range (x, "all"), 26);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! x(3) = NaN;
%! assert (range (x, "all", "omitnan"), 26);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! x(3) = NaN;
%! assert (range (x, "all", "includenan"), NaN);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! x(3) = NaN;
%! s = min (x, [], "includenan");
%! l = max (x, [], "includenan");
%! assert (range (x, "includenan"), l - s);
%!test
%! x = reshape (1:27, [3, 3, 3]);
%! x(3) = NaN;
%! y = range (x, 3, "includenan");
%! assert (y(3), NaN);

## Test input validation
%!error <Invalid call> range ()
%!error <Invalid call> range (1, 2, 3, 4)
%!error <range: X must be a numeric array> range (['A'; 'B'])
%!error <range: X must be a numeric array> range ({1, 2})