rescale
Scale range of array elements
Description
scales all elements in R
= rescale(A
)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
.
specifies options using one or more name-value arguments in addition to any of the
input argument combinations in previous syntaxes. For example,
R
= rescale(___,Name,Value
)rescale(A,"InputMin",5)
sets all elements in
A
that are less than 5 to 5 before scaling to the interval
[0, 1].
Examples
Scale to Unit Interval
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
Specify Interval
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
Specify Interval and Range for Matrix Columns
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
Specify Range
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
A
— Input array
vector | matrix | multidimensional array
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
).
l
— Lower bound
0 (default) | scalar | vector | matrix | multidimensional array
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.
u
— Upper bound
1 (default) | scalar | vector | matrix | multidimensional array
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)
InputMin
— Minimum of input range
scalar | vector | matrix | multidimensional array
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))
InputMax
— Maximum of input range
scalar | vector | matrix | multidimensional array
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 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
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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
rescale
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.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2017b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)