Arg max of a function
Afficher commentaires plus anciens
I have a doubt regarding finding arg max of a function m = x+y?
function m = equation(x,y)
k = x+y;
[max_value, max_ind] = max(k);
m = k(max_ind);
end
Thank you in advance
6 commentaires
dpb
le 21 Nov 2020
Well, yes, you've tried to compare the LHS to the RHS via the equality "==" operator for some reason that is not at all clear although the immediate error is owing to the fact that neither variable referenced on the LHS is defined.
We don't know what you expect the function to do or return -- there's quite a lot wrong here besides beginning with the minor detail that equation is missspelled altho MATLAB doesn't care, it'll cause difficulty in remembering to use the misspellled version if continue.
But, you pass arguments x,y into the function but immediately overwrite them with local variables -- whassup w/ that?
There's really no way to proceed from this -- tell us what you're trying to do and include a sample of inputs and expected output(s) to help with the explanation.
Tina poddar
le 21 Nov 2020
dpb
le 22 Nov 2020
But x+y is just a 2D array of constant values -- what do you think is some maximum or function? The question still makes no sense.
And what do you mean, precisely, by the phrase "arg max" that you keep using?
Tina poddar
le 22 Nov 2020
Bruno Luong
le 22 Nov 2020
No, argmax is the argument at which the function (in your case the array result of plus) is maximized (hence the name ARGMAX)
You should return "max_ind" or "sub2ind(size(k),max_ind)" rather than k(max_ind) (which is btw equal to max_value).
Tina poddar
le 28 Nov 2020
Réponses (1)
Steven Lord
le 21 Nov 2020
1 vote
The operator + does not and cannot return two output arguments. Nor can its functional form, plus.
As a hint, take a look at the examples on the documentation page for the max function and see if you can adapt one of those to your needs.
1 commentaire
Tina poddar
le 22 Nov 2020
Catégories
En savoir plus sur Multidimensional Arrays dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!