Python | Numpy matrix.clip ()

With the Numpy matrix.clip () method we can select elements from the matrix by passing the parameter as minimum value and maximum value. The output array with a range between these parameters and the size of the matrix will be the same.

Syntax: matrix.clip()

Return: Return a matrix having value in between min and max values.

Example # 1:
In this example, we can see that using the matrix.clip () method we can crop the matrix between the minimum value and the maximum value.

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( ` [1, 2, 3, 4; 3, 1, 5, 6] ` < code class = "plain">)

 
# applying the matrix.clip method ()

geeks = gfg.clip ( 1 , 4 )

  

print (geeks)

Output:

 [[1 2 3 4] [3 1 4 4]] 

Example # 2:

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[ 1, 2, 3; 4, 5, 6; 7, 8, 9] ` )

  
# using the matrix.clip () method

geeks = gfg.clip ( 4 , 9 )

 

print (geeks)

Exit:

 [[4 4 4] [4 5 6] [7 8 9]]