how do i find argmax of, say x=[1 2] without any user defined function?

 Réponse acceptée

Walter Roberson
Walter Roberson le 21 Mai 2013

7 votes

[argvalue, argmin] = min(x);
[argvalue, argmax] = max(x);

1 commentaire

shikhar
shikhar le 21 Mai 2013
Modifié(e) : shikhar le 21 Mai 2013
thanks a lot Mr.Roberson

Connectez-vous pour commenter.

Plus de réponses (2)

Lukasz Laszko
Lukasz Laszko le 28 Oct 2019
Modifié(e) : Lukasz Laszko le 28 Oct 2019

4 votes

I'm afraid the answer by Walter Roberson is not correct. According to docs the second returned value contains indices of the minimum/maximum values of x. So to find argmax, you need to call x(max_ind),
eg.
x=0.1:.01:4*pi;
[max_val max_ind]=max(cos(x))
argmax=x(max_ind)
-->> max_val=1
-->> argmax=2pi
-->> max_ind=619

3 commentaires

Walter Roberson
Walter Roberson le 17 Mai 2021
is Q1T a function? You have Q1T(0) which would not work if Q1T were a variable being indexed.
James Tursa
James Tursa le 17 Mai 2021
Modifié(e) : James Tursa le 18 Mai 2021
@Lukasz Laszko Argmax is defined as the elements of the domain (i.e., in this case the indexes) where the function (i.e., in this case the vector) obtains the maximum. See these definitions:
Walter's answer is correct according to these definitions.
Your use of cos(x) introduces another function inbetween that was not part of the original question. Did the original question have cos(x) that was subsequently edited out?
In the case a function that is effectively f(x(K)) and what you wanted was the x(K) then it would not be unreasonable to take
fx = f(x);
[~, argidx] = max(fx);
argmax = x(argidx);
This adds an extra level of indirection, but it would be a common one.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by