finding the max values and their coordinates/indices
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(4 个)
the cyclist
2014-9-3
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 个评论
Guillaume
2014-9-3
"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 个评论
Image Analyst
2014-9-3
"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 个评论
per isakson
2014-9-3
编辑:per isakson
2014-9-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!