How can I find the index of the maximum element in an array without looping?
For example, if I have:
a = [1 2 999 3];
I want to define a function indexMax so that indexMax(a) would return 3.
Likewise for defining indexMin.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As pointed by Evgeni
maxandmincan return theargmaxandargminas second arguments.It is worth while noting that you can use these functions along specific dimensions:
Note the empty
[]second argument – it is crucialmax( A, [], 2 )is not at all equivalent tomax( A, 2 )(I’ll leave it to you as a small exercise to see whatmax( A, 2 )does).The argmax/argmin returned from these “along dimension” calls are row/col indices.