Previous Up Next

7.1.4  The median: median

Although the average of a list of numbers typically means the mean, there are other notions of “average”. Another one is the median; the median of a list of numbers is the middle number when they are listed in numeric order. For example, the median of the list [1,2,5,7,20] is simply 5. If the length of a list of numbers is even, so there isn’t a middle number, the median is then the mean of the two middle numbers; for example, the median of [1,2,5,7,20,21] is (5+7)/2 = 6.

The median function finds the median of a list. The command

median([1,2,5,7,20])

will return

5

The median function can take weights with a second argument, where the weight of number represents how many times it is counted in a list. For example,

median([1,2,5,7,20],[5,3,2,1,2])

will return

2

since the median of 1,1,1,1,1,2,2,2,5,5,7,20,20 is 2.


Previous Up Next