Median of a matrix
Write a NumPy program to find the median of all values in a matrix.
[[4, 8, 1], [2, 9, 6]]
5.0
Approach
np.median.
Solution
import numpy as npm = np.array([[4, 8, 1], [2, 9, 6]])print(np.median(m))5.0
Line by line
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.