MATLAB: Minimum value in sub Matrix

MATLABminimummultidimensional

I have a 4 dimensional matrix that represents an array of arrays and I need to find the minimum value and its index in each of the sub arrays. IE I need to call min(example(:)) on example(:,:,1,1), example(:,:,1,2)… example(:,:,1,m), example(:,:,2,1)… example(:,:,2,m)… example(:,:,n,m). Any help with this would be much appreciated.

Best Answer

You can just use min with its optional dimension argument:
X = [4D array];
Y = min(min(X,[],1),[],2);
Here is a simple example, with a 3D array:
>> X = reshape(1:24, [2,3,4])
X(:,:,1) =
1 3 5
2 4 6
X(:,:,2) =
7 9 11
8 10 12
X(:,:,3) =
13 15 17
14 16 18
X(:,:,4) =
19 21 23
20 22 24
>> Y = min(min(X,[],1),[],2)
Y(:,:,1) =
1
Y(:,:,2) =
7
Y(:,:,3) =
13
Y(:,:,4) =
19
You can see then it has calculated the minimum of the first two dimensions only, leaving the third dimension remaining. This works for 4D arrays too. If you do not want the leading scalar-dimensions to remain, simply use squeeze to remove them:
>> squeeze(Y)
ans =
1
7
13
19