# how do i find argmax?

870 views (last 30 days)
shikhar on 21 May 2013
Edited: James Tursa on 18 May 2021
how do i find argmax of, say x=[1 2] without any user defined function?

Walter Roberson on 21 May 2013
[argvalue, argmin] = min(x);
[argvalue, argmax] = max(x);
shikhar on 21 May 2013
Edited: shikhar on 21 May 2013
thanks a lot Mr.Roberson

Lukasz Laszko on 28 Oct 2019
Edited: Lukasz Laszko on 28 Oct 2019
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
Walter Roberson on 18 May 2021
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.

Konstantin on 12 Jun 2018
Great!!!!