How can I use the multidimensional index returned by max or min?

Viewed 64

In matlab,

[M,I] = max(A, [], dim) 

returns maximum values and the index of those maximum values along the specified dimension dim. When A is an multidimensional array, variable I turns out to be a multidimensional array with same dimension as A, except the dim dimension set to 1. My question is how do I use the index multidimensional array I to get back the max from the A.

To provide a specific use case, I want to obtain the complex number with maximum real part along the dimension dim, when A could contain complex numbers.

I can use

[M,I] = max(real(A), [], dim) 

to get the maximum real parts and their indices. But, how do I get the original complex variable at those indices? I am particularly interested in cases where A has more than 2 dimensions.

1 Answers
Related