minimum of a 3D matrix

M=rand(2,2,3)
[minM idx] = min(M(:));
[n m t] = ind2sub(size(M),idx)
minM
M(n,m,t)
I know this will give the minimum of the M matrix.
However, I want minimum value only for each M(:,:,i) part.
Is there anyway to do that?
Thanks in advance!

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 31 Oct 2012
Edited: Azzi Abdelmalek on 31 Oct 2012
M=rand(2,2,3);
out=min(reshape(min(M),size(M,2),[],1))'
or
out=arrayfun(@(x) min(min(M(:,:,x))),1:size(M,3))'
If you want to get the corresponding index
M=rand(6,4,5);
[n,m,p]=size(M)
[c,idx]=min(M)
[c1,idx2]=min(c)
idx1=arrayfun(@(x) idx(1,idx2(1,1,x),x),(1:p)')
v=[c1(:) idx1(:) idx2(:) (1:p)' ]

More Answers (1)

Try this:
minOfPlane = min(min(M(:,:, t)))

9 Comments

Qian
Qian on 31 Oct 2012
If i code as follows, M=rand(3,3,2);
[minM idx]=min(min(M(:,:,1)));
[n m t]=ind2sub(size(M),idx);
I don't know why n,m is not giving the right indices.
If you want to do all the planes simultaneously, min(min(M)) . You might want to squeeze() the result to make it a column vector.
Qian
Qian on 31 Oct 2012
M=rand(3,3,2);
[minM idx]=min(min(M));
[n m t]=ind2sub(size(M),idx);
It seems like n, m will only give the indices for minimum for the last part of matrix, not each plane simultaneously.
[minM idx] = min(reshape(M(:,:,1),[],1)); %first plane only
or, for all simultaneously,
[minM idx] = min(reshape(M, [], size(M,3)));
Qian
Qian on 31 Oct 2012
so in this case idx will never give the index as a*b form.
[minM idx] = min(reshape(M, [], size(M,3)));
idx = idx + (size(M,1)*size(M,2)).*(0:2);
[n m t] = ind2sub(size(M), idx);
Qian
Qian on 31 Oct 2012
that's brilliant, i need time to work on that. thank you so much!!!!!
[v, i1] = min(reshape(M,[],size(M,3)));
min_idx_m_n_t = [rem(i1-1,size(M,1))+1; ceil(i1/size(M,1)); 1:size(M,3)]';
Ah yes, the 0:2 I used should be 0:size(M,3)-1

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!