MATLAB Answers

How to find the index of the median of a matrix.

216 views (last 30 days)
Ellie
Ellie on 14 Dec 2015
Commented: Walter Roberson on 4 Nov 2020
Hi All,
Just like the function [M, I] = max(a) where a is matrix, I need to do this for median. The matrix that I will be testing will always be odd, so there is no need to be able to calculate for even.
Thanks.

Answers (3)

Andrew McLaren
Andrew McLaren on 26 Apr 2018
Old thread, but here's one way to do it if you input a vector instead of an array. If you have an odd number of values, this will find the index of the first entry of the median. If you have an even number, it will find the index of the first number which is equally close to the median. (I.e. if your list has a median of 5 and contains 4,4,6,6 the first 4 will be reported.) It also reports how close your value is to the median.
a = rand(100,1); %the array in question
[y idx] = min(abs(a-median(a)))
  2 Comments
Walter Roberson
Walter Roberson on 4 Nov 2020
median() has two possibilities:
  • if there are an odd number of entries, then the median is exactly one of the values
  • if there is an even number of entries, then the median is the mean of the two middle values. If the two middle values are the same, then the median will come out exactly equal to the (equal) values; if the two middle values are not the same, then the median will come out as something not exactly equal to any of the entries
In the first case, since the median will be exactly one of the values, a-median(a) will be exactly 0 for all the entries exactly equal to the median, and abs() of that will be 0, and min() of values that are non-negative (because of the abs) will be 0 (written into y) and idx will be the position of the first such value.
In the second case, if the two middle values are the same, then you get the same situation as above, abs(0) and idx will be the position of the first such value. If the two middle values were not the same, then the median is algebraically equally spaced from two values in the array, and abs() of the difference would algebraically have two values the same distance from the median, and idx would be to the first of the two. In practice, the rounding could be different for the two subtractions, so one of the abs() could come out slighty less than the other, and the index would be to that one.

Sign in to comment.


Chad Greene
Chad Greene on 14 Dec 2015
For some matrix a:
a = randi(100,5);
The median value in a is given by
a_med = median(a(:));
A logical array the size of a containing ones wherever a is equal to its mean is given by
med_logical = a==a_med;
Indices of the ones in med_logical are given by
find(med_logical)
Or, putting it all together,
find(a==median(a(:)))
  2 Comments
Walter Roberson
Walter Roberson on 14 Dec 2015
M = median(matrix);
I = find(matrix == M, 1, 'first');
If you have several copies of the same value then this will output the index of the first of the copies.

Sign in to comment.


Chad Greene
Chad Greene on 14 Dec 2015
Create your own function
function [M,I] = mymedian(A,dim)
if nargin==1
dim = 1;
end
M = median(A,dim);
I = find(A==M);
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by