我有一个尺寸为100*10*1344的3D matlab矩阵.
我想找到矩阵的最大元素的三个索引.
当我尝试使用命令find找到它时,我得到:
>> [i j k]=find(max(A(:))==A)
i =
52
j =
9601
k =
1
Run Code Online (Sandbox Code Playgroud)
但使用这些索引会得到以下结果:
>> A(i ,j, k)
??? Index exceeds matrix dimensions.
Run Code Online (Sandbox Code Playgroud)
如何解决问题?
你不能有find三个指数,只有两个.第三个输出是值,而不是索引.
我建议你得到一个单一的索引,然后是一个线性索引.您可以直接使用它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
Run Code Online (Sandbox Code Playgroud)
此外,如果您只需要第一次出现的最大值(如果有多个),您可以使用第二个输出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
Run Code Online (Sandbox Code Playgroud)