Main Content

range

Range of values

Description

y = range(X) returns the difference between the maximum and minimum values of sample data in X.

  • If X is a vector, then range(X) is the range of the values in X.

  • If X is a matrix, then range(X) is a row vector containing the range of each column in X.

  • If X is a multidimensional array, then range operates along the first nonsingleton dimension of X, treating the values as vectors. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same. If X is an empty array with first dimension 0, then range(X) returns an empty array with the same size as X.

example

y = range(X,'all') returns the range of all elements in X.

example

y = range(X,dim) returns the range along the operating dimension dim of X. For example, if X is a matrix, then range(X,2) is a column vector containing the range value of each row.

example

y = range(X,vecdim) returns the range over the dimensions specified in the vector vecdim. For example, if X is a matrix, then range(X,[1 2]) is the range of all elements in X because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

Examples

collapse all

Generate five large samples of standard normal random numbers.

rng('default') % For reproducibility
rv = normrnd(0,1,1000,5);

Find the range values of the samples.

near6 = range(rv)
near6 = 1×5

    6.8104    6.6420    6.9578    6.0860    6.8165

The range value is approximately 6 for each sample.

Find the range of exam scores over the course of five exams.

Load the examgrades data set.

load examgrades

Find the range of all exam scores.

y = range(grades,'all')
y = 
46

Find the range of exam scores for each student over the course of five exams.

Load the examgrades data set.

load examgrades

Find the range of exam scores for the first 10 students. For example, the difference between the eighth student's best and worst exam scores is 7 points.

X = grades(1:10,:);
y = range(X,2)
y = 10×1

    12
    13
    10
    12
     8
    16
    14
     7
    12
    10

Find the range of a multidimensional array over multiple dimensions.

Create a 3-by-5-by-2 array of normal random numbers with mean mu = 2 and standard deviation sigma = 7.

rng('default') % For reproducibility
mu = 2;
sigma = 7;
X = normrnd(mu,sigma,[3 5 2])
X = 
X(:,:,1) =

    5.7637    8.0352   -1.0351   21.3861    7.0778
   14.8372    4.2314    4.3984   -7.4492    1.5586
  -13.8119   -7.1538   27.0488   23.2445    7.0032


X(:,:,2) =

    0.5652   11.8632   -6.4524    5.4223   -0.1241
    1.1310   11.9203    7.0207    9.2429    4.0571
   12.4279    6.7005   13.4116    7.0882   -3.5110

Find the range of each page of X by specifying dimensions 1 and 2 as the operating dimensions.

ypage = range(X,[1 2])
ypage = 
ypage(:,:,1) =

   40.8607


ypage(:,:,2) =

   19.8641

For example, ypage(1,1,2) is the range of all the elements in X(:,:,2).

Find the range of the elements in each X(i,:,:) slice by specifying dimensions 2 and 3 as the operating dimensions.

yrow = range(X,[2 3])
yrow = 3×1

   27.8385
   22.2864
   40.8607

For example, yrow(3) is the range of all the elements in X(3,:,:).

Input Arguments

collapse all

Data sample, specified as a scalar, vector, matrix, or multidimensional array.

  • If X is a scalar, then range(X) is 0.

  • If X is a 0-by-0 empty array, then range(X) is also an empty array.

Data Types: single | double | logical | datetime | duration

Dimension to operate along, specified as a positive integer scalar. If you do not specify a value, then the default value is the first array dimension whose size does not equal 1.

dim indicates the dimension whose length reduces to 1. size(y,dim) is 1, while the sizes of all other dimensions remain the same unless size(X,dim) is 0. If size(X,dim) is 0, then range(X,dim) returns an empty array of the same size as X.

Consider a two-dimensional data sample X:

  • If dim is equal to 1, then range(X,1) returns a row vector containing the range for each column.

  • If dim is equal to 2, then range(X,2) returns a column vector containing the range for each row.

If dim is greater than ndims(X), range returns an array of zeros with the same dimensions and missing values as X.

Data Types: single | double

Vector of dimensions, specified as a positive integer vector. Each element of vecdim represents a dimension of the input array X. The output y has length 1 in the specified operating dimensions. The other dimension lengths are the same for X and y.

For example, if X is a 2-by-3-by-3 array, then range(X,[1 2]) returns a 1-by-1-by-3 array. Each element of the output array is the range of the elements on the corresponding page of X.

Mapping of input dimension of 2-by-3-by-3 to output dimension of 1-by-1-by-3

Data Types: single | double

Output Arguments

collapse all

Difference between the maximum and minimum values, returned as a scalar, vector, matrix, or multidimensional array.

Tips

  • range treats NaNs as missing values and ignores them.

  • range provides an easily calculated estimate of the spread of a sample. Avoid using range with data that has outliers because they have an undue influence on this statistic.

Extended Capabilities

Version History

Introduced before R2006a

See Also

| |