2016-07-24 207 views
2

我有一個尺寸爲100 * 10 * 1344的3D matlab矩陣。如何查找3D MATLAB矩陣中最大元素的位置?

我想找到矩陣的最大元素的三個指標。

當我嘗試用find命令找到它,我得到:

>> [i j k]=find(max(A(:))==A) 
i = 
    52 
j = 
    9601 
k = 
    1 

但是,使用這些指標提供了以下結果:

>> A(i ,j, k) 
??? Index exceeds matrix dimensions. 

如何解決這個問題?

回答

6

你不能讓find返回三個索引,只有兩個。第三個輸出是值,而不是索引。

我建議你得到一個單一的索引,這將是一個linear index。您可以直接將其用於A,或使用ind2sub轉換爲三個索引。

實施例:

A = rand(3,4,5); % example 2D array 
ind = find(max(A(:))==A(:)); 
A(ind) % use linear index directly into A 
[ii, jj, kk] = ind2sub(size(A), ind); % or convert to three indices... 
A(ii, jj, kk) % ...and use them into A 

此外,如果只需要最大的第一次出現(如果有不止一個),就可以使用的max代替find第二輸出:

A = rand(3,4,5); % example 2D array 
[~, ind] = max(A(:)); % second output of this function gives position of maximum 
A(ind) % use linear index directly into A 
[ii, jj, kk] = ind2sub(size(A), ind); % or convert to three indices... 
A(ii, jj, kk) % ...and use them into A 
+1

非常感謝! –