# find min or max value element from more than two matrices

35 views (last 30 days)
Sanaz on 2 Jun 2014
Commented: Sanaz on 2 Jun 2014
I know how we can find min or max value element from two matrices. For more than two matrices we can do two by two and then compare but I was wondering if there is any other simple way to find min or max value element from more than two matrices?

George Papazafeiropoulos on 2 Jun 2014
Edited: George Papazafeiropoulos on 2 Jun 2014
% data
matrix1=rand(2);
matrix2=rand(3);
matrix3=rand(4);
% engine
c=nan(4,4,3);
c(1:2,1:2,1)=matrix1;
c(1:3,1:3,2)=matrix2;
c(1:4,1:4,3)=matrix3;
% result
d=min(c,[],3)
e=max(c,[],3)
Sanaz on 2 Jun 2014
I changed it a little bit and I used that like: a=rand(3,3); b=rand(3,3); c=rand(3,3); d(:,:,1)=a;d(:,:,2)=b;d(:,:,3)=c; and the minimum=min(d,[],3) (the result has the same size of a or b or c).

Image Analyst on 2 Jun 2014
For 3 matrices, you can do
minValue = min([a(:);b(:);c(:)])
maxValue = max([a(:);b(:);c(:)])
It's easy to see how to adapt it for more matrices.
##### 2 CommentsShow 1 older commentHide 1 older comment
Star Strider on 2 Jun 2014
Much more efficient.
+1