フィルターのクリア

Finding index to minimum values in 3D array

4 ビュー (過去 30 日間)
KAE
KAE 2018 年 3 月 8 日
コメント済み: KAE 2018 年 3 月 9 日
I have a 3D matrix, and I would like to find the index to the minimum value along the 3rd dimension. In other words, I would like to replace the following loop with a faster operation:
A = rand(10,8,3);
indexToMinIn3rdDim = NaN*ones(size(A,1), size(A,2));
for iRow = 1:size(A,1)
for iCol = 1:size(A,2)
indexToMinInVector = find((squeeze(A(iRow,iCol,:))) == min((squeeze(A(iRow,iCol,:)))));
indexToMinInVector = indexToMinInVector(1); % Only keep 1st index if the same min value occurs
indexToMinIn3rdDim(iRow,iCol) = indexToMinInVector;
end
end

採用された回答

Rik
Rik 2018 年 3 月 9 日
The isequal function thinks the code below works.
A=rand(10,8,3);
[a,b]=min(A,[],3);
indexToMinIn3rdDim = NaN*ones(size(A,1), size(A,2));
for iRow = 1:size(A,1)
for iCol = 1:size(A,2)
indexToMinInVector = find((squeeze(A(iRow,iCol,:))) == min((squeeze(A(iRow,iCol,:)))));
indexToMinInVector = indexToMinInVector(1); % Only keep 1st index if the same min value occurs
indexToMinIn3rdDim(iRow,iCol) = indexToMinInVector;
end
end
isequal(indexToMinIn3rdDim,b)
  1 件のコメント
KAE
KAE 2018 年 3 月 9 日
Great! And easy to understand too.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by