Quantcast

Documentation Center

  • Trial Software
  • Product Updates

median

Median value of array

Syntax

Description

example

M = median(A) returns the median value of A.

  • If A is a vector, then median(A) returns the median value of A.

  • If A is a nonempty matrix, then median(A) treats the columns of A as vectors and returns a row vector of median values.

  • If A is an empty 0-by-0 matrix, median(A) returns NaN.

  • If A is a multidimensional array, then median(A) acts along the first nonsingleton dimension and returns an array of median values. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.

median computes natively in the numeric class of A, such that class(M) = class(A).

example

M = median(A,dim) returns the median of elements along dimension dim. For example, if A is a matrix, then median(A,2) is a column vector containing the median value of each row.

Examples

expand all

Median of Matrix Columns

Define a 4-by-3 matrix.

A = [0 1 1; 2 3 2; 1 3 2; 4 2 2]
A =

     0     1     1
     2     3     2
     1     3     2
     4     2     2

Find the median value of each column.

M = median(A)
M =

    1.5000    2.5000    2.0000

For each column, the median value is the mean of the middle two numbers in sorted order.

Median of Matrix Rows

Define a 2-by-3 matrix.

A = [0 1 1; 2 3 2]
A =

     0     1     1
     2     3     2

Find the median value of each row.

M = median(A,2)
M =

     1
     2

For each row, the median value is the middle number in sorted order.

Median of 3-D Array

Create a 1-by-3-by-4 array of integers between 1 and 10.

A = gallery('integerdata',10,[1,3,4],1)
A(:,:,1) =

    10     8    10


A(:,:,2) =

     6     9     5


A(:,:,3) =

     9     6     1


A(:,:,4) =

     4     9     5

Find the median values of this 3-D array along the second dimension.

M = median(A)
M(:,:,1) =

    10


M(:,:,2) =

     6


M(:,:,3) =

     6


M(:,:,4) =

     5

This operation produces a 1-by-1-by-4 array by computing the median of the three values along the second dimension. The size of the second dimension is reduced to 1.

Compute the median along the first dimension of A.

M = median(A,1);
isequal(A,M)
ans =

     1

This returns the same array as A because the size of the first dimension is 1.

Median of 8-bit Integer Array

Define a 1-by-4 vector of 8-bit integers.

A = int8(1:4)
A =

    1    2    3    4

Compute the median value.

M = median(A),
class(M)
M =

    3


ans =

int8

M is the mean of the middle two numbers in sorted order returned as an 8-bit integer.

Input Arguments

expand all

A — Input arrayvector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array.

If A contains NaN, then M returns NaN.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, the default is the first array dimension whose size does not equal 1.

Dimension dim indicates the dimension whose length reduces to 1. The size(M,dim) is 1, while the sizes of all other dimensions remain the same.

Consider a two-dimensional input array, A.

  • If dim = 1, then median(A,1) returns a row vector containing the median of the elements in each column.

  • If dim = 2, then median(A,2) returns a column vector containing the median of the elements in each row.

median returns A if dim is greater than ndims(A).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

More About

expand all

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1.

For example:

  • If X is a 1-by-n row vector, then the second dimension is the first nonsingleton dimension of X.

  • If X is a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension of X.

  • If X is a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension of X.

See Also

| | | | | | |

Was this topic helpful?