# rescale

Scale range of array elements

## Description

scales the entries of `R`

= rescale(`X`

)`X`

to the interval [0,1]. The output array
`R`

is the same size as `X`

.

specifies additional parameters for rescaling using one or more name-value
arguments. For example, `R`

= rescale(___,`Name,Value`

)`rescale(X,"InputMin",5)`

sets all elements
in `X`

that are less than 5 equal to 5 before scaling to the range
[0,1].

## Examples

### Scale to Unit Interval

Scale the entries of a vector to the interval [0,1].

X = 1:5; R = rescale(X)

`R = `*1×5*
0 0.2500 0.5000 0.7500 1.0000

### Scale to Specified Range

Scale the elements of a vector to the interval [-1,1].

X = 1:5; R = rescale(X,-1,1)

`R = `*1×5*
-1.0000 -0.5000 0 0.5000 1.0000

### Scale Matrix Columns and Rows

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 values of `InputMin`

and `InputMax`

.

X = magic(3)

`X = `*3×3*
8 1 6
3 5 7
4 9 2

colmin = min(X)

`colmin = `*1×3*
3 1 2

colmax = max(X)

`colmax = `*1×3*
8 9 7

Rcol = rescale(X,"InputMin",colmin,"InputMax",colmax)

`Rcol = `*3×3*
1.0000 0 0.8000
0 0.5000 1.0000
0.2000 1.0000 0

Scale each row of `X`

to the interval [0,1].

rowmin = min(X,[],2)

`rowmin = `*3×1*
1
3
2

rowmax = max(X,[],2)

`rowmax = `*3×1*
8
7
9

Rrow = rescale(X,"InputMin",rowmin,"InputMax",rowmax)

`Rrow = `*3×3*
1.0000 0 0.7143
0 0.5000 1.0000
0.2857 1.0000 0

## Input Arguments

`X`

— Input array

scalar | vector | matrix | multidimensional array

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

If

`X`

has type`single`

, then the output also has type`single`

. Otherwise, the output has type`double`

.If

`X`

is constant, then`rescale`

returns the lower bound of the interval (0 by default) or`NaN`

(when the specified interval contains`Inf`

).

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

`a`

— Lower bound

0 (default) | scalar | vector | matrix | multidimensional array

Lower bound, specified as a scalar, vector, matrix, or multidimensional
array. `a`

must have a size that is compatible with the
input array. For example, if `X`

is an
* M*-by-

*matrix, then*

`N`

`rescale`

operates along the dimension determined by
the shape of `a`

:If

`a`

is a scalar, then`rescale`

uses it as the lower bound for all elements of`X`

.If

`a`

is a 1-by-row vector, then`N`

`rescale`

uses each element as the lower bound for the corresponding column of`X`

.If

`a`

is an-by-1 column vector, then`M`

`rescale`

uses each element as the lower bound for the corresponding row of`X`

.

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`

`b`

— Upper bound

1 (default) | scalar | vector | matrix | multidimensional array

Upper bound, specified as a scalar, vector, matrix, or multidimensional
array. `b`

must have a size that is compatible with the
input array. For example, if `X`

is an
* M*-by-

*matrix, then*

`N`

`rescale`

operates along the dimension determined by
the shape of `b`

:If

`b`

is a scalar, then`rescale`

uses it as the upper bound for all elements of`X`

.If

`b`

is a 1-by-row vector, then`N`

`rescale`

uses each element as the upper bound for the corresponding column of`X`

.If

`b`

is an-by-1 column vector, then`M`

`rescale`

uses each element as the upper bound for the corresponding row of`X`

.

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`

### 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.

*
Before R2021a, use commas to separate each name and value, and enclose*
`Name`

*in quotes.*

**Example: **```
B =
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 for an input array
`X`

is `min(X(:))`

. 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 value of
`InputMin`

before scaling.

The value of `InputMin`

must have a size that is
compatible with the input array. For example, if `X`

is
an * M*-by-

*matrix, then*

`N`

`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`X`

.If the input minimum is a 1-by-

row vector, then`N`

`rescale`

uses each element as the minimum for the corresponding column of`X`

.If the input minimum is an

-by-1 column vector, then`M`

`rescale`

uses each element as the minimum for the corresponding row of`X`

.

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 range

scalar | vector | matrix | multidimensional array

Maximum of input range, specified as a scalar, vector, matrix, or
multidimensional array. The default value for an input array
`X`

is `max(X(:))`

. 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 value of
`InputMax`

before scaling.

The value of `InputMax`

must have a size that is
compatible with the input array. For example, if `X`

is
an * M*-by-

*matrix, then*

`N`

`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`X`

.If the input maximum is a 1-by-

row vector, then`N`

`rescale`

uses each element as the maximum for the corresponding column of`X`

.If the input maximum is an

-by-1 column vector, then`M`

`rescale`

uses each element as the maximum for the corresponding row of`X`

.

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`

## Algorithms

`R = rescale(X,a,b,"InputMin",inmin,"InputMax",inmax)`

uses the
formula

`l + [(X-inmin)./(inmax-inmin)].*(b-a)`

to scale the elements of an array `X`

when the values of
`X`

are within the bounds of `inmin`

and
`inmax`

.

If

`a`

and`b`

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(X(:))`

.If

`InputMax`

is not specified, then`rescale`

sets its value to the default`max(X(:))`

.

## Extended Capabilities

### Tall Arrays

Calculate with arrays that have more rows than fit in memory.

Usage notes and limitations:

The inputs

`a`

and`b`

, and the value of the name-value arguments`InputMin`

and`InputMax`

, 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™.

This function fully supports GPU arrays. 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**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## 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)