MATLAB: How to find multiple maximum values and they are indexes of 4-D array

maximum valuen-d or multidimensional array

Hi every body, pleas who can help me to find the multiple values and they indexes of 4-D array ?
as an example below :
A(:,:,1,1)=
1 3 5
3 5 1
5 1 3
I want the output as :
max value= 5 , index (3,1,1,1)
max value= 5 , index (2,2,1,1)
max value= 5 , index (1,3,1,1)
My attempt to solve that is :
[max_val, Index] = max(A(:));
[A1,A2,A3,A4] = ind2sub(size(A),Index);
the output was:
max value= 5 , index =3
A1= 3
A2= 1
A3= 1
A4= 1
Which means only the first maximum value, but I want all values. Thank you in advance

Best Answer

Hi,
You can try something like this:
maxval = max(A(:));
idx = find(A == maxval);
[A1,A2,A3,A4] = ind2sub(size(A),idx);
This should give all the max value indices.