larray.Array.clip

Array.clip(self, minval=None, maxval=None, out=None)[source]

Clip (limit) the values in an array.

Given an interval, values outside the interval are clipped to the interval bounds. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

Parameters:
minval : scalar or array-like, optional

Minimum value. If None, clipping is not performed on lower bound. Defaults to None.

maxval : scalar or array-like, optional

Maximum value. If None, clipping is not performed on upper bound. Defaults to None.

out : Array, optional

The results will be placed in this array.

Returns:
Array

An array with the elements of the current array, but where values < minval are replaced with minval, and those > maxval with maxval.

Notes

  • At least either minval or maxval must be defined.
  • If minval and/or maxval are array_like, broadcast will occur between self, minval and maxval.

Examples

>>> arr = ndtest((3, 3)) - 3
>>> arr
a\b  b0  b1  b2
 a0  -3  -2  -1
 a1   0   1   2
 a2   3   4   5
>>> arr.clip(0, 2)
a\b  b0  b1  b2
 a0   0   0   0
 a1   0   1   2
 a2   2   2   2

Clipping on lower bound only

>>> arr.clip(0)
a\b  b0  b1  b2
 a0   0   0   0
 a1   0   1   2
 a2   3   4   5

Clipping on upper bound only

>>> arr.clip(maxval=2)
a\b  b0  b1  b2
 a0  -3  -2  -1
 a1   0   1   2
 a2   2   2   2

clipping using bounds which vary along an axis

>>> lower_bound = Array([-2, 0, 2], 'b=b0..b2')
>>> upper_bound = Array([0, 2, 4], 'b=b0..b2')
>>> arr.clip(lower_bound, upper_bound)
a\b  b0  b1  b2
 a0  -2   0   2
 a1   0   1   2
 a2   0   2   4