Find the layer the minimum number occurs

I have a 3D array and I want to find the layer where the minimum number occurs for each row,column combination. Where the output is a 2D array of the same number of rows/columns as the input 3D array.

 Accepted Answer

Try this:
M = rand(4, 5, 3) % Create Matrix
[Mmin,Idx] = min(M,[],3) % Return ‘Page’ Of Minimum
It takes the minimum along the third dimension, and returns the ‘page’ (in the ‘Idx’ output) where the minimum occurs.

More Answers (1)

array=randi(10,4,4,3);
[rows colm z]=size(array);
for j=1:z;
num(j)=sum(sum(array(:,:,j)==1)+sum(array(:,:,j)==2)+sum(array(:,:,j)==3));
end
minimum_value=min(num);
idx=find(num==minimum_value);
fprintf('The Layer number is %d',idx)

Community Treasure Hunt

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

Start Hunting!