NCL Home > Documentation > Functions > General applied math, Variable manipulators

# scale_values

Scale the values of an array to a user specified range.

Available in version 6.6.0 and later.

## Prototype

```load "\$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"  ; This library is automatically loaded
; from NCL V6.2.0 onward.
; No need for user to explicitly load.

function scale_values (
x          : numeric,
minRet [1] : numeric,
maxRet [1] : numeric,
opt    [1] : logical
)

return_val  :  float or double
```

## Arguments

x

A numeric array of any dimensionality.

minRet

Scalar specifying the minimum value of the return array.

maxRet

Scalar specifying the maximum value of the return array.

opt

Currently, not used. Set to False.

## Return value

A float or double array of the same size and shape as the input array. If present, coordinate meta data is returned.

## Description

The following is used to scale the array values into a user specified range:

```
xMin   = min(x)
xMax   = max(x)
xNew   = minRet + (maxRet-minRet)*((x-xMin)/(xMax-xMin))
```

## Examples

Example 1: The entire array is scaled.

```       x      = random_normal( 20, 8, (/20,40/))
printMinMax(x, 0)      ; min=-2.6165   max=43.2392

; create new variable

xScale = scale_values(x, -2, 2, False)
printMinMax(xScale, 0) ; min=-2   max=2

xScale = scale_values(x, 3.5, 13.2, False)
printMinMax(xScale, 0) ; min=3.5   max=13.2

; overwrite current variable

x = scale_values(x, -2, 2, False)
printMinMax(x, 0) ; min=-2   max=2
```
Example 2: Consider: x(ntim,nlat,mlon). Scale values at each time step:
```
; new variable scaled by values at each time step
xscale = new( dimx, typeof(x), getFillValue(x))
do nt-0,ntim-1
xscale(nt,:,:) = scale_values(x(nt,:,:), loNew, hiNew, opt)
end do

; overwrite current variable

do nt-0,ntim-1
x(nt,:,:) = scale_values(x(nt,:,:), loNew, hiNew, opt)
end do
```