Matlab

# Median in MATLAB

This article explains how to use the median() function in MATLAB. We show you the syntax, the input and output arguments, and the various ways this function can be used to process the results.

This article also includes a working example for each function call type, with code snippets you can copy and paste into the MATLAB command line to better understand how median() works.

We have included images showing the results in the MATLAB environment for each example.

## MATLAB Median Function Syntax

m = median (a)
m = median (a, ‘all’)
m = median (a, dim)
m = median (a, vecdim)
m = median (___, nanflag)

## MATLAB Median Function description

MATLAB median() function is used to compute the median value of all values in an array. The median returns the average value of “a” in “m”. This function works with 2D and multidimensional arrays and offers the possibility of using flags to select in which dimension to calculate the median value of the input array. Like most functions of this type in MATLAB, median() also provides the option to ignore or not the NaN values contained in the input array. Below, you will see a list of each of the input flags of this function. We will explain in detail what each of them is for.

dim: This input specifies the dimension along which median() is to determine the median value of the array “a”. The data type of this input is a positive integer scalar.

vecdim: This is the dimension vector by which the dimensions are selected from which median() is to determine the median value for each of them. The data type for this input is a vector of positive integers.

nanflag: This flag specifies how the median() function should handle NaN values. The includenan flag includes these values in the results, while omitnan omits them and returns 0 instead.

all: If the function median() is called with this flag, it returns a scalar with the average value of all elements of the array “a”.

## How To Get the Median Value Between the Elements of a Vector With the MATLAB Function median()

In this example, we will see how to obtain the average value between all elements of the vector “v”. To do this, we create a vector with positive real values and enter it as an input argument in median(). As a result, this function returns a scalar with the average value of the vector “v”.

v= [3, 8, 2, 3, 1, 5, 9, 4, 6  ];
m = median ( v )

The following figure shows the result where median() returns in “m” a scalar with the median value of all elements of the vector “v”:

## How To Get the Unique Median Value Between All Elements of an Array With the Flag “all” of the MATLAB Function median()

In the following example, we will see how to find the unique median value of all elements of an array. To do this, we create the array “a” with 4×4 elements and send it as the input argument of median() along with the flag “all”, separated by commas, as follows:

a = [ 3, 18, 69,  7;
25, 96,  2,  3;
1, 48, 57, 78;
35, 16, 73, 44 ];

m = median ( a, ‘all’)

As seen in the following figure, median() in “m” returns the unique average value resulting from all values of the array “a”:

## How To Get the Median Values of the Individual Columns of a Matrix Using the “dim” Input of the MATLAB Function median()

Now, let us look at how to get the median value of each row of an array using the MATLAB median function. When this function is called with an empty “dim” input, the median defaults to working with the columns in dimension 1 of the input array. So, median() returns a row vector with the mean of each column of “a”. In this example, we use the array “a” in the previous example and pass it as the input argument, leaving the “dim” input empty, as shown in the following image:

a = [ 3, 18, 69,  7;
25, 96,  2,  3;
1, 48, 57, 78;
35, 16, 73, 44 ];

m = median ( a )

As the figure shows, if the input “dim” is empty, median() operates by default with dimension 1 of the array and returns a row vector with the mean value of “a” in “m”.

## How To Get the Median Value of Each Row of an Array With the median() Function Using the “dim” Input

In this example, we will look at how we can use MATLAB median function to obtain the median value of each row. To do this, we use the array from the previous example, which serves as the input array. Then, call the median() function by specifying the dimension “2” in the input “dim”, as shown in the following figure:

a = [ 3, 18, 69,  7;
25, 96,  2,  3;
1, 48, 57, 78;
35, 16, 73, 44 ];

m = median ( a, 2)

As shown in the following figure, median() returns a column vector with the median value of each row of the array “a”:

## How To Include or Omit NaN Values in the Results of the MATLAB median() Function

Like many MATLAB functions, median() allows the inclusion or exclusion of NaN values in the results. This function includes these values by default. Next, we will see how you can contain or exclude these values using the “includenan” and “omitnan” flags of the “nanflag” input. In the first example, we used the “nanflag” input to specify that NaN values are included in the results. In the second example, we specified that these values are omitted from the results.

a = [ NaN, 2 , NaN, NaN;
25, 96,   2,   3;
1, 48, NaN,  78;
35, 16,  73, NaN ];

m = median ( a, 'includenan')

m =

NaN  32  NaN  NaN

The following figure shows that median() has included NaN values in its results:

In the following example, the “omitnan” flag excluded NaN values from the results:

a = [ NaN, 2 , NaN, NaN;
25, 96,   2,   3;
1, 48, NaN,  78;
35, 16,  73, NaN ];

m = median ( a, 'omitnan')

m =

25.0000 32.0000 37.5000 40.5000

## Conclusion

This article explained how to use the median() function in MATLAB. We showed you the syntax, the input and output arguments, and the various ways the results can be processed. Also, for each function call type, we have included a working example with code snippets that you can copy and paste into the MATLAB command line. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.