Finding index to minimum values in 3D array

4 次查看(过去 30 天)
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)

更多回答(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