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

95 次查看(过去 30 天)
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.

回答(4 个)

Andrew McLaren
Andrew McLaren 2018-4-26
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 个评论
Sophia Sperry
Sophia Sperry 2020-11-3
Can you explain how this works to find the position? I'm trying to understand how this is working.
Walter Roberson
Walter Roberson 2020-11-4
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
Chad Greene 2015-12-14
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 个评论
Ellie
Ellie 2015-12-14
Its not quite working how I expected. Ideally it would work exactly like the this:
[M, I] = max(matrix)
Walter Roberson
Walter Roberson 2015-12-14
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
Chad Greene 2015-12-14
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

Kevin Beck
Kevin Beck 2021-7-5
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 个评论
Walter Roberson
Walter Roberson 2021-7-5
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by