finding the max values and their coordinates/indices
11 views (last 30 days)
Show older comments
MATLAB documentation says
"[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I."
But that does not seem to work. See below.
X = [2 8 4;
7 3 9];
[a,b] = max(X)
a =
7 8 9
b =
2 1 2
The indices could have been given as linear indices b = [ 2 3 6] so that X(b) would give the max values of columns of X in a.
It looks like an error in MATLAB because the doc says something but it does something different.
0 Comments
Answers (4)
the cyclist
on 3 Sep 2014
This is behaving exactly as expected to me. It is returning the max value of each column, and the row index (for each column). I agree that the documentation is not crystal clear, though.
0 Comments
Guillaume
on 3 Sep 2014
"If A is a matrix, then max(A) treats the columns of A as vectors and returns a row vector of largest elements."
The index is in respect to each column. You can easily transform the indices returned into what you want:
b_seetha = b + (0:size(X, 1):numel(X)-1);
0 Comments
Image Analyst
on 3 Sep 2014
"a" already gives "the max values of columns of X" so why would you need to use X(b)?
R2014a documentation says "[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned." I agree that "indices" is not clear. They meant indices of each column of A as if each column were it's own column vector, while you were hoping that indices mean "linear indices" which is often used elsewhere. That certainly could be clarified. You can submit a request to have the documentation clarified here: https://mathworksservicerequest.secure.force.com/cp_case_new?cc=us which is what you get when you go here and click "Report a bug".
0 Comments
per isakson
on 3 Sep 2014
Edited: per isakson
on 3 Sep 2014
I believe that max behaves as intended and that the documentation doesn't describe the function properly.
Try
>> m = magic( 5 );
>> [ c, ix ] = max(m(:));
>> [ I1, I2] = ind2sub( size(m), ix )
I1 =
5
I2 =
3
>> m(I1,I2)
ans =
25
>>
Two lines are needed to answer the question in the title.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!