Numpy matrix.max() - Python
The matrix.max() method find the maximum value in a matrix.
Syntax
matrix.max()
Return: Returns the maximum value from the given matrix.
Examples
Example 1: In this example, we create a 2×2 NumPy matrix and use matrix.max() to get largest value in the matrix.
import numpy as np
m = np.matrix('[64, 1; 12, 3]')
mx = m.max()
print(mx)
Output
64
Explanation:
- np.matrix('[64, 1; 12, 3]') creates a 2×2 matrix
- m.max() scans all elements in the matrix to find the maximum value.
Example 2: Here, we create a 3×3 matrix and use matrix.max() to determine the largest number.
import numpy as np
m = np.matrix('[1, 2, 3; 4, 5, 6; 7, 8, 9]')
mx = m.max()
print(mx)
Output
9
Explanation:
- np.matrix('[1, 2, 3; 4, 5, 6; 7, 8, 9]') creates the matrix
- m.max() iterates over all elements and identifies the largest number.