Skip to content

Median ​

The middle value of the collection once sorted.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageMedian✓
Median(collection)the middle value of the collection once sorted.
SageMathmedian(collection)
Median(matrix)the column-wise median, one value per column.
SageMathmedian(matrix)
Median(dist)the exact quantile at 1/2 of a distribution, where one exists; otherwise unevaluated.
SageMathmedian(dist)
Typed signatures
TypePackageApplies
((collection<nothing | number> | number)+) -> nan | real | signed_infinitycompute-enginealways
((collection<nothing | number> | list<list<any>> | number)*) -> list | nan | real | signed_infinitycombinatoricsreplaces compute-enginealways
((collection<nothing | number> | distribution | list<list<any>> | number)*) -> list | nan | number | real | signed_infinitystatisticsreplaces combinatoricsalways

Domain: Collections

Details
  • For an odd-length collection, the median is the middle element of Sort's result; for even length, it's the average of the two middle elements.
  • Much less sensitive to outliers than Mean — a single extreme value barely moves it.
  • Given a matrix (a list of equal-length rows), computes the median of each column.
  • See Mode for the most frequent value.

Examples

Basic #
live ↗
Inmedian([1,2,3,4,5])\mathrm{median}(\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack)Out33
live ↗
Inmedian([1,2,3,4])\mathrm{median}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out52\frac{5}{2}
live ↗
Inmedian([1,2,3,4,5,6,7])\mathrm{median}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7\bigr\rbrack)Out44
live ↗
Inmedian([1,2,3,4,5,6,7,8])\mathrm{median}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8\bigr\rbrack)Out92\frac{9}{2}
Scope #

Given a matrix, Median is column-wise

live ↗
Inmedian([[1,11,3],[4,6,7]])\mathrm{median}(\bigl\lbrack\bigl\lbrack1, 11, 3\bigr\rbrack, \bigl\lbrack4, 6, 7\bigr\rbrack\bigr\rbrack)Out[52,172,5]\bigl\lbrack\frac{5}{2}, \frac{17}{2}, 5\bigr\rbrack

Unsorted data is sorted first; the middle pair 3 and 4 averages to 72\frac{7}{2}

live ↗
Inmedian([3,1,4,1,5,9,2,6])\mathrm{median}(\bigl\lbrack3, 1, 4, 1, 5, 9, 2, 6\bigr\rbrack)Out72\frac{7}{2}

Exact rationals

live ↗
Inmedian([12,13,16])\mathrm{median}(\bigl\lbrack\frac{1}{2}, \frac{1}{3}, \frac{1}{6}\bigr\rbrack)Out13\frac{1}{3}

Exact constants are ordered by value, putting ee in the middle

live ↗
Inmedian([π,e,2])\mathrm{median}(\bigl\lbrack π, e, 2\bigr\rbrack)Outee

The median of a lazy Range of even length is the mean of the middle two

live ↗
Inmedian(1..100)\mathrm{median}(1..100)Out1012\frac{101}{2}

The median of a 30-digit numeric list is a float

live ↗
Inmedian(N([1,2,3,4],30))\mathrm{median}(N(\bigl\lbrack1, 2, 3, 4\bigr\rbrack, 30))Out2.52.5

Columnwise over a three-dimensional array of data

live ↗
Inmedian([[[3,7],[2,1]],[[5,19],[12,4]]])\mathrm{median}(\bigl\lbrack\bigl\lbrack\bigl\lbrack3, 7\bigr\rbrack, \bigl\lbrack2, 1\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigl\lbrack5, 19\bigr\rbrack, \bigl\lbrack12, 4\bigr\rbrack\bigr\rbrack\bigr\rbrack)Out[[4,13],[7,52]]\bigl\lbrack\bigl\lbrack4, 13\bigr\rbrack, \bigl\lbrack7, \frac{5}{2}\bigr\rbrack\bigr\rbrack
Applications #

The same outlier-laden data as in Mean's example: the median of 1 barely moves, unlike the mean's −383-\frac{38}{3}

live ↗
Inmedian([−100,1,1,1,1,20])\mathrm{median}(\bigl\lbrack-100, 1, 1, 1, 1, 20\bigr\rbrack)Out11

Robust to the outlier 100000100000, unlike the mean

live ↗
Inmedian([1,5,2,6,10,100 000,5,4])\mathrm{median}(\bigl\lbrack1, 5, 2, 6, 10, 100\,000, 5, 4\bigr\rbrack)Out55
Properties #

For an odd-length list, the median is the middle element of the sorted list. See Sort

live ↗
Inmedian([5,3,1,4,2])=Sort([5,3,1,4,2])[3]\mathrm{median}(\bigl\lbrack5, 3, 1, 4, 2\bigr\rbrack)=\mathrm{Sort}(\bigl\lbrack5, 3, 1, 4, 2\bigr\rbrack)[3]OutTrue\mathrm{True}

Even count: the average of the two middle values, 22 and 33

live ↗
InN(median([1,2,3,4,4,3,1,1]))N(\mathrm{median}(\bigl\lbrack1, 2, 3, 4, 4, 3, 1, 1\bigr\rbrack))Out2.52.5

Stays an exact rational for exact data

live ↗
Inmedian([1,2,3,4,4,3,1,1])\mathrm{median}(\bigl\lbrack1, 2, 3, 4, 4, 3, 1, 1\bigr\rbrack)Out52\frac{5}{2}
About
Examples16
Checked againstWolfram Language 16 agreeSageMath 12 agree2 error
CrosswalkWolfram LanguageSageMath

Implementation

mappedsagepackages/oracle/src/mappings.ts

See also: Mean, Mode