Reputation: 25
i have a little question about MATLAB.
I have a 3D-array where each 2D-layer is a picture. What i need is the "third" index (=the number of the layer) of the lowest value for each pixel in this array. At the end i want to have a 2D array where i have the index of these lowest values.
An example:
3D array with these 2 2D-layers:
layer 1:
3-5-6
1-4-2
7-5-2
layer 2:
1-8-2
4-6-1
9-2-5
The result should be the following array:
2-1-2
1-1-2
1-2-1
I hope you can see what i want to achieve, sorry for my bad English, I cannot really express my problem in words...
Thank you for your help.
Upvotes: 0
Views: 1580
Reputation: 9864
Let A
be the matrix then you can use
[B, I] = min(A, [], 3);
Where B
is the minimum values and I
contains the indices. If you are not interested in minimum values you can use
[~, I] = min(A, [], 3);
For you example
>> A(:,:,1) = [3 5 6; 1 4 2; 7 5 2];
>> A(:,:,2) = [1 8 2; 4 6 1; 9 2 5];
>> [B,I]=min(A, [], 3)
B =
1 5 2
1 4 1
7 2 2
I =
2 1 2
1 1 2
1 2 1
Upvotes: 5