0
$\begingroup$

Say I am tabling the values of a two variable function using a list of indexes (which I can't assume to be ordered), I'd like to return the index (or the list of indexes) which yield the maximum value(s) for one of the two arguments (supplying the other one externally.

indexes = {1,5,19,3,2,14};
f[x_,y_] := someFunc[x,y]; 
(* Just a place holder, any function returning integers should do, if there are multiple maxima I'd like a list in return and I'll break ties later. *)
ArgMax[Table[ f[x,y],{x,indexes}]];
$\endgroup$
  • 1
    $\begingroup$ Is MaximalBy the answer? $\endgroup$ – Kuba May 7 '18 at 11:00
  • $\begingroup$ Ostensibly yes, but in fact my function takes more values. What would be the syntax in that case? Would something like this work? MaximalFunc[list_,y_] := MaximalBy[list,f[y,#2]]; $\endgroup$ – Three Diag May 7 '18 at 11:17
  • $\begingroup$ Takes them from where? I'd say MaximalBy[indexes, f[#, externalVar, ...]&] $\endgroup$ – Kuba May 7 '18 at 11:21
1
$\begingroup$

As explained by @Kuba in a comment the answer is simply to use MaximalBy[]

OptimalIndexes[y_] = MaximalBy[indexes,f[#,y]&];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.