필터 지우기
필터 지우기

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by