finding the max values and their coordinates/indices
19 ビュー (過去 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!