Python Practice › NumPy Programs › Program 66

Median of a matrix

EasyNumPy Programs · 66 of 100

Write a NumPy program to find the median of all values in a matrix.

Advertisement
Input
[[4, 8, 1], [2, 9, 6]]
Output
5.0

Approach

  1. np.median.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs