This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Median value of a data sample

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


stats::median(x1, x2, …, <Averaged>)
stats::median([x1, x2, …], <Averaged>)
stats::median(s, <c>, <Averaged>)


stats::median(x1, x2, …) returns the median of the data xi.

The median of n sorted values x1 ≤ … ≤ xn is .

The averaged median of n sorted values x1 ≤ … ≤ xn is .

For odd n, both the median and the averaged median coincide with the element of the sorted data list. For even n, the median is , whilst the averaged median is .

The median coincides with the -quantile of the data: the calls stats::median(data <Averaged>) and stats::empiricalQuantile(data)(1/2 <Averaged>) are equivalent. See the help page of stats::empiricalQuantile for details on the parameters specifying the data.

The column index c is optional if the data are given by a stats::sample object containing only one non-string column. Cf. Example 3.

External statistical data stored in an ASCII file can be imported into a MuPAD® session via import::readdata. In particular, see Example 1 of the corresponding help page.


Example 1

We calculate the median of a sequence of five values:

stats::median(3, 8, 5, 9/2, 11)

Alternatively, data may be passed as a list:

stats::median([2, 7, 3, 9/2, 11, 12]),
stats::median([2, 7, 3, 9/2, 11, 12], Averaged)

Example 2

We create a sample:

stats::sample([[4, 7, 5], [3, 6, 17], [8, 2, 2]])
4  7   5
3  6  17
8  2   2

The median of the second column is 6:

stats::median(%, 2)

Example 3

We create a sample consisting of one string column and one non-string column:

stats::sample([["1996", 1242], ["1997", 1353], ["1998", 1142]])
"1996"  1242
"1997"  1353
"1998"  1142

The median of the second column is calculated. In this case, there is no need to specify the index of the column, since it is the only non-string data column in the sample:



x1, x2, …

The statistical data: real numerical values.


A sample of domain type stats::sample


A column index of the sample s: a positive integer. This column provides the data x1, x2 etc.



Return the averaged median value

Return Values

arithmetical expression. FAIL is returned if the data sample is empty.

Was this topic helpful?