Main Content

rescale

Scale range of array elements

Description

example

R = rescale(A) scales all elements in A to the interval [0, 1] according to the minimum and maximum over all elements in A. The output array R is the same size as A.

example

R = rescale(A,l,u) scales all elements in A to the interval [l u].

example

R = rescale(___,Name,Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example, rescale(A,"InputMin",5) sets all elements in A that are less than 5 to 5 before scaling to the interval [0, 1].

Examples

collapse all

Scale the elements of a vector to the unit interval [0, 1], which is the default interval for rescale. Scaling preserves the shape of the distribution.

A = 1:5;
R = rescale(A)
R = 1×5

         0    0.2500    0.5000    0.7500    1.0000

Scale the elements of a vector to the interval [–1, 1] by specifying the lower and upper bounds.

A = 1:5;
R = rescale(A,-1,1)
R = 1×5

   -1.0000   -0.5000         0    0.5000    1.0000

Independently scale each column of a matrix to the unit interval [0, 1]. Specify the minimum of the input range as a row vector containing the minimum element in each matrix column. Specify the maximum of the input range as a row vector containing the maximum element in each matrix column.

A = [0.4 -4; 0.5 -5; 0.9 9; 0.2 1]
A = 4×2

    0.4000   -4.0000
    0.5000   -5.0000
    0.9000    9.0000
    0.2000    1.0000

colmin = min(A)
colmin = 1×2

    0.2000   -5.0000

colmax = max(A)
colmax = 1×2

    0.9000    9.0000

R = rescale(A,"InputMin",colmin,"InputMax",colmax)
R = 4×2

    0.2857    0.0714
    0.4286    0.0000
    1.0000    1.0000
         0    0.4286

Scale the second column to the interval [–1, 1]. Specify the upper and lower bounds for the rescaled data in addition to the InputMin and InputMax name-value arguments.

Rcol = rescale(A,[0 -1],1,"InputMin",colmin,"InputMax",colmax)
Rcol = 4×2

    0.2857   -0.8571
    0.4286   -1.0000
    1.0000    1.0000
         0   -0.1429

Clip the elements of the input vector to the range [1, 5], and rescale the vector to the default interval [0, 1]. Clipping restricts all element values to the specified input range.

A = [-30 1 2 3 4 5 70];
R = rescale(A,"InputMin",1,"InputMax",5)
R = 1×7

         0         0    0.2500    0.5000    0.7500    1.0000    1.0000

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

  • If A has type single, then the output also has type single. Otherwise, the output has type double.

  • If A is constant, then rescale returns the lower bound of the interval (0 by default) or NaN (when the specified interval contains Inf).

Lower bound for the rescaled data, specified as a scalar, vector, matrix, or multidimensional array. l must be less than the upper bound and have a size that is compatible with the input array. For more information, see Compatible Array Sizes for Basic Operations.

To use the same lower bound for all elements of A, specify l as a scalar. To use different lower bounds for each column or row in A, specify l as a row or column vector, respectively.

If you set different interval bounds for each column or row, rescale still considers all values in the input array when calculating scaled values for each column or row. To rescale each column or row independently, in addition to specifying l and u as vectors, set the range of the input array along each column or row by specifying the InputMin and InputMax name-value arguments as vectors.

Upper bound for the rescaled data, specified as a scalar, vector, matrix, or multidimensional array. u must be greater than the lower bound and have a size that is compatible with the input array. For more information, see Compatible Array Sizes for Basic Operations.

To use the same upper bound for all elements of A, specify u as a scalar. To use different upper bounds for each column or row in A, specify u as a row or column vector, respectively.

If you set different interval bounds for each column or row, rescale still considers all values in the input array when calculating scaled values for each column or row. To rescale each column or row independently, in addition to specifying l and u as vectors, set the range of the input array along each column or row by specifying the InputMin and InputMax name-value arguments as vectors.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: R = rescale(A,InputMin=5,InputMax=10)

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: R = rescale(A,"InputMin",5,"InputMax",10)

Minimum of input range, specified as a scalar, vector, matrix, or multidimensional array. The default value of InputMin is min(A(:)). InputMin must have a size that is compatible with the input array. For more information, see Compatible Array Sizes for Basic Operations.

Specify InputMin to clip or expand the range of the input array. rescale uses InputMin as the minimum of the input range instead of the minimum of A. Elements of A that are less than InputMin are set to the corresponding value of InputMin before scaling.

To use the same input range minimum for all elements of A, specify InputMin as a scalar. To scale columns of A independently, specify InputMin as a row vector. To scale rows of A independently, specify InputMin as a column vector.

Example: R = rescale(A,"InputMin",5)

Example: R = rescale(A,"InputMin",min(A),"InputMax",max(A))

Maximum of input range, specified as a scalar, vector, matrix, or multidimensional array. The default value of InputMax is max(A(:)). InputMax must have a size that is compatible with the input array. For more information, see Compatible Array Sizes for Basic Operations.

Specify InputMax to clip or expand the range of the input array. rescale uses InputMax as the maximum of the input range instead of the maximum of A. Elements of A that are greater than InputMax are set to the corresponding value of InputMax before scaling.

To use the same input range maximum for all elements of A, specify InputMax as a scalar. To scale columns of A independently, specify InputMax as a row vector. To scale rows of A independently, specify InputMax as a column vector.

Example: R = rescale(A,"InputMax",10)

Example: R = rescale(A,"InputMin",min(A),"InputMax",max(A))

Algorithms

rescale uses the formula R=l+[Ainputmininputmaxinputmin](ul) to scale the elements of the input array A when the values of A are within the range defined by InputMin and InputMax.

  • If l and u are not specified, then rescale uses the default values 0 and 1, respectively.

  • If InputMin is not specified, then rescale sets its value to the default min(A(:)).

  • If InputMax is not specified, then rescale sets its value to the default max(A(:)).

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017b

See Also

Functions

Apps