MATLAB: Identifying x,y coordinate of the peak value value in a 3d array

3d arrayind2sub

Hello All,
I have a 3d_array of size (95, 99, 27), and my goal is to find out the position (x, y) of maximum value in each page i.e. (:,:,1) to (:,:,27). I have used the following commands to achieve this:
[max_value_output, index_max] = max(max(abs(output(:,:,ii))));
This command helps me identify the maximum value in the page (i.e. (:,:,1)) and also the corresponding column number of the max value. In the next step, I try to find out the row number (y value) using this command:
[xpeak, ypeak] = ind2sub(size(output(:,:,ii)),index_max);
This gives me an erroneous y value (I plotted a surface plot to manually identify the position of the peak). Could you please provide guidance on how to identify the correct row number (y value). Thanks in advance.
Cheers, Danny

Best Answer

[m,n,k] = size(output);
[max_value_output,ii] = max(reshape(output,[],k));
[xpeak, ypeak] = ind2sub([m,n],ii);
out = [max_value_output,xpeak, ypeak];
Related Question