Scale range of array elements
scales the entries of an array to the interval [0,1]. The output array
B
= rescale(A
)B
is the same size as A
.
specifies additional parameters for scaling an array for either of the previous
syntaxes. For example, B
= rescale(___,Name,Value
)rescale(A,'InputMin',5)
sets all elements
in A
that are less than 5 equal to 5 before scaling to the range
[0,1].
Scale the entries of a vector to the interval [0,1].
A = 1:5; B = rescale(A)
B = 1×5
0 0.2500 0.5000 0.7500 1.0000
Scale the elements of a vector to the interval [-1,1].
A = 1:5; B = rescale(A,-1,1)
B = 1×5
-1.0000 -0.5000 0 0.5000 1.0000
Scale each column of a matrix to the interval [0,1] by specifying the minimum and maximum of each column. rescale
scales along the dimension of the input array that corresponds with the shape of the 'InputMin'
and 'InputMax'
parameter values.
A = magic(3)
A = 3×3
8 1 6
3 5 7
4 9 2
colmin = min(A)
colmin = 1×3
3 1 2
colmax = max(A)
colmax = 1×3
8 9 7
Bcol = rescale(A,'InputMin',colmin,'InputMax',colmax)
Bcol = 3×3
1.0000 0 0.8000
0 0.5000 1.0000
0.2000 1.0000 0
Scale each row of A
to the interval [0,1].
rowmin = min(A,[],2)
rowmin = 3×1
1
3
2
rowmax = max(A,[],2)
rowmax = 3×1
8
7
9
Brow = rescale(A,'InputMin',rowmin,'InputMax',rowmax)
Brow = 3×3
1.0000 0 0.7143
0 0.5000 1.0000
0.2857 1.0000 0
A
— Input arrayInput array, specified as a scalar, 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 a scalar, then
rescale
returns the lower bound of the
interval (0 by default) or NaN
(when the
output range contains Inf
).
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
l
— Lower boundLower bound, specified as a scalar, vector, matrix, or multidimensional
array. l
must have a size that is compatible with the
input array. For example, if A
is an
M-by-N matrix, then
rescale
operates along the dimension dictated by the
shape of l
:
If l
is a scalar, then
rescale
uses it as the lower bound for
all elements of A
.
If l
is a 1-by-N row
vector, then rescale
uses each element as the
lower bound for the corresponding column of
A
.
If l
is an M-by-1 column
vector, then rescale
uses each element as the
lower bound for the corresponding row of
A
.
For more information on compatible array sizes, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
u
— Upper boundUpper bound, specified as a scalar, vector, matrix, or multidimensional
array. u
must have a size that is compatible with the
input array. For example, if A
is an
M-by-N matrix, then
rescale
operates along the dimension dictated by the
shape of u
:
If u
is a scalar, then
rescale
uses it as the upper bound for
all elements of A
.
If u
is a 1-by-N row
vector, then rescale
uses each element as the
upper bound for the corresponding column of
A
.
If u
is an M-by-1 column
vector, then rescale
uses each element as the
upper bound for the corresponding row of
A
.
For more information on compatible array sizes, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
B =
rescale(A,'InputMin',5,'InputMax',10)
'InputMin'
— Minimum of input rangeMinimum of input range, specified as a scalar, vector, matrix, or
multidimensional array. The default value for an input array
A
is min(A(:))
. Specifying an
input range either expands or shrinks the range of the input data. For
instance, rescale
sets all elements that are less
than the specified input minimum to the 'InputMin'
value before scaling.
The 'InputMin'
value must have a size that is
compatible with the input array. For example, if A
is
an M-by-N matrix, then
rescale
operates along the dimension dictated by
the shape of the input minimum:
If the input minimum is a scalar, then
rescale
uses that minimum value for
all elements of A
.
If the input minimum is a 1-by-N row
vector, then rescale
uses each element as
the minimum for the corresponding column of
A
.
If the input minimum is an M-by-1
column vector, then rescale
uses each
element as the minimum for the corresponding row of
A
.
For more information on compatible array sizes, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
'InputMax'
— Maximum of input rangeMaximum of input range, specified as a scalar, vector, matrix, or
multidimensional array. The default value for an input array
A
is max(A(:))
. Specifying an
input range either expands or shrinks the range of the input data. For
instance, rescale
sets all elements that are greater
than the specified input maximum to the 'InputMax'
value before scaling.
The 'InputMax'
value must have a size that is
compatible with the input array. For example, if A
is
an M-by-N matrix, then
rescale
operates along the dimension dictated by
the shape of the input maximum:
If the input maximum is a scalar, then
rescale
uses that maximum value for
all elements of A
.
If the input maximum is a 1-by-N row
vector, then rescale
uses each element as
the maximum for the corresponding column of
A
.
If the input maximum is an M-by-1
column vector, then rescale
uses each
element as the maximum for the corresponding row of
A
.
For more information on compatible array sizes, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
B = rescale(A,l,u,'InputMin',inmin,'InputMax',inmax)
uses the
formula
l + [(A-inmin)./(inmax-inmin)].*(u-l)
to scale the elements of an array A
when the values of
A
are within the bounds of inmin
and
inmax
.
If l
and u
are not specified, then
rescale
uses the default values 0 and 1,
respectively.
If the 'InputMin'
name-value pair is not specified,
then rescale
sets its value to the default
min(A(:))
.
If the 'InputMax'
name-value pair is not specified,
then rescale
sets its value to the default
max(A(:))
.
Usage notes and limitations:
The inputs l
and u
, and the value of the name-value pairs 'InputMin'
and 'InputMax'
, cannot have more than one row.
For more information, see Tall Arrays.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
You have a modified version of this example. Do you want to open this example with your edits?