How to find the index of the median of a matrix.
95 views (last 30 days)
Show older comments
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.
0 Comments
Answers (4)
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
Sophia Sperry
on 3 Nov 2020
Can you explain how this works to find the position? I'm trying to understand how this is working.
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.
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
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.
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
0 Comments
Kevin Beck
on 5 Jul 2021
noob to Matlab so this took me a couple of hours, and it's not tight yet, just the core of the idea...
>> a = [5 3 7 12 5 0 3 4];
>> b = cumsum(a);
>> i = find(b>b(end)/2);
>> i = i(1)
i =
4
1 Comment
Walter Roberson
on 5 Jul 2021
a = [1 2 20]
cumsum is b=[1 3 23]
then b(end)=23 and b(end)/2 is 11.5
find(b>11.5)
is going to return 3
That implies that the median of [1 2 20] is 20
Perhaps you then think that you should take the location before that, find(b<=b(end)/2,1,'last')
But then
a = [1 2 3 10 20]
b = [1 3 6 16 36]
find(a<=36/2,1,'last')
would pick out the location of the 10 and so imply that the median is 10 when instead the median is 3
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!