rescale - Scale range of array elements - MATLAB (original) (raw)
Scale range of array elements
Syntax
Description
`R` = rescale([A](#d126e1566973))
scales all elements in A
to the interval [0, 1] according to the minimum and maximum over all elements in A
. The output arrayR
is the same size as A
.
`R` = rescale([A](#d126e1566973),[l](#d126e1567018),[u](#d126e1567073))
scales all elements in A
to the interval [l u]
.
`R` = rescale(___,[Name,Value](#namevaluepairarguments))
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 inA
that are less than 5 to 5 before scaling to the interval [0, 1].
Examples
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.
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 = 1×2
0.2000 -5.0000
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
Input array, specified as a vector, matrix, or multidimensional array.
- If
A
has typesingle
, then the output also has typesingle
. Otherwise, the output has typedouble
. - If
A
is constant, thenrescale
returns the lower bound of the interval (0 by default) orNaN
(when the specified interval containsInf
).
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
andInputMax
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
andInputMax
name-value arguments as vectors.
Name-Value Arguments
Specify optional pairs of arguments asName1=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, seeCompatible Array Sizes for Basic Operations.
Specify InputMin
to clip or expand the range of the input array. rescale
usesInputMin
as the minimum of the input range instead of the minimum of A
. Elements ofA
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 ofA
, specify InputMin
as a scalar. To scale columns of A
independently, specifyInputMin
as a row vector. To scale rows ofA
independently, specifyInputMin
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, seeCompatible Array Sizes for Basic Operations.
Specify InputMax
to clip or expand the range of the input array. rescale
usesInputMax
as the maximum of the input range instead of the maximum of A
. Elements ofA
that are greater thanInputMax
are set to the corresponding value ofInputMax
before scaling.
To use the same input range maximum for all elements ofA
, specify InputMax
as a scalar. To scale columns of A
independently, specifyInputMax
as a row vector. To scale rows ofA
independently, specifyInputMax
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+[A−inputmininputmax−inputmin](u−l) to scale the elements of the input array A
when the values of A
are within the range defined byInputMin
and InputMax
.
- If
l
andu
are not specified, thenrescale
uses the default values 0 and 1, respectively. - If
InputMin
is not specified, thenrescale
sets its value to the defaultmin(A(:))
. - If
InputMax
is not specified, thenrescale
sets its value to the defaultmax(A(:))
.
Extended Capabilities
Therescale
function supports tall arrays with the following usage notes and limitations:
- The inputs
l
andu
, and the value of the name-value argumentsInputMin
andInputMax
, cannot have more than one row.
For more information, see Tall Arrays.
The rescale
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2017b