My MMA fu is very stale, and I don't seem to be able to see a simple way to apply a function across a subset of a list. Specifically in the following data, I want to apply Max to the second element of each pair in each line - hope that makes sense?:

{{100., 20.}, {99.7401, 19.7457}},
{{102., 22.}, {98.7401, 39.7457}},
{{103., 21.}, {97.7401, 49.7457}},

Ideally I'd expect the result to be

res={20.,39.7457, 49.7457}

Appreciate any hints tips or incantations. These lists are large so I'd appreciate any suggestions about how to improve performance.

  • 3
    $\begingroup$ Max[#[[All, -1]]] & /@ lst? $\endgroup$ – J. M. is away Nov 8 '17 at 0:33
  • 3
    $\begingroup$ apparently slightly faster than @J.M. 's suggestion: Max /@ lst[[All, All, 2]] $\endgroup$ – M. Stern Nov 8 '17 at 1:14
  • 2
    $\begingroup$ Generally Query is slow but it's convenient for hierarchical manipulations like this: Query[All, Max, 2] @ lst $\endgroup$ – mfvonh Nov 8 '17 at 1:24
  • $\begingroup$ Btw.: Welcome to Mathematica.StackExchange! $\endgroup$ – Henrik Schumacher Nov 8 '17 at 8:37

Many thanks for the prompt responses. All three of the suggestions worked. The one that felt most familiar and apparently may be the fastest was:

Max /@ lst[[All, All, 2]]

The other approaches are worth collating just in case it helps anyone else:

Max[#[[All, -1]]] & /@ lst
Query[All, Max, 2] @ lst

Again thank you for the prompt and helpful responses.


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.