Python Practice › NumPy Programs › Program 66
Median of a matrix
Write a NumPy program to find the median of all values in a matrix.
Input
[[4, 8, 1], [2, 9, 6]]
Output
5.0
Approach
np.median.
Solution
import numpy as npm = np.array([[4, 8, 1], [2, 9, 6]])print(np.median(m))Output
5.0
Line by line
Line 3
Sorted values are 1 2 4 6 8 9, so the median is (4 + 6) / 2 = 5.0.
Learn the concept
New to this topic? Read the lesson, then come back.