MaximalBy

MaximalBy[{e1,e2,},f]

returns a list of the ei for which the value of f[ei] is maximal.

MaximalBy[{e1,e2,},f,n]

returns a list of the ei corresponding to the n largest f[ei].

MaximalBy[f]

represents an operator form of MaximalBy that can be applied to an expression.

Details

  • Values of f[ei] are compared using the same canonical order as in Sort.
  • The maximal ei are returned in the order they appear in the input.
  • In the case of MaximalBy[list,f,n], the ei are sorted in the order of decreasing f[ei], with those having the same value of f[ei] being taken in the order they appear in list.
  • MaximalBy[list,f, UpTo[n]] gives n elements, or as many as are available.
  • MaximalBy[f][expr] is equivalent to MaximalBy[expr,f].
  • In MaximalBy[assoc,f,], f is applied to the values of the association assoc.

Examples

open allclose all

Basic Examples  (4)

Find the maximal element by its last part:

All maximal elements are returned, in order of appearance:

Obtain the first three maximal elements:

Obtain the first four maximal elements, or as many as are available:

Scope  (1)

MaximalBy works with symbolic expressions, using OrderedQ:

Properties & Relations  (1)

MaximalBy[{e1,e2,},f,n] compares values f[ei] using canonical Order:

TakeLargestBy[{e1,e2,},f,n] compares values f[ei] using NumericalOrder:

Possible Issues  (1)

The maximal element is determined using OrderedQ, not numerical ordering:

Compare numerical values of the elements of the list:

Wolfram Research (2014), MaximalBy, Wolfram Language function, https://reference.wolfram.com/language/ref/MaximalBy.html (updated 2015).

Text

Wolfram Research (2014), MaximalBy, Wolfram Language function, https://reference.wolfram.com/language/ref/MaximalBy.html (updated 2015).

CMS

Wolfram Language. 2014. "MaximalBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/MaximalBy.html.

APA

Wolfram Language. (2014). MaximalBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MaximalBy.html

BibTeX

@misc{reference.wolfram_2023_maximalby, author="Wolfram Research", title="{MaximalBy}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/MaximalBy.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_maximalby, organization={Wolfram Research}, title={MaximalBy}, year={2015}, url={https://reference.wolfram.com/language/ref/MaximalBy.html}, note=[Accessed: 19-March-2024 ]}