Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my approach using <code>Pick</code></p> <pre><code>maxBy[list_, n_] := With[{s = list[[All, n]]}, Pick[list, s, Max[s]]] maxBy[{{4, 3}, {5, 10}, {-2, 1}, {3, 7}}, 2] (* output: {{5, 10}} *) </code></pre> <p>This version works on any number of elements per sublist provided <code>n</code> is less than or equal to the length of the shortest sublist. </p> <p>Timings for this version on my machine</p> <pre><code>list2 = RandomInteger[{-10^7, 10^7}, {10^6, 2}]; list3 = RandomInteger[{-10^7, 10^7}, {10^6, 3}]; list9 = RandomInteger[{-10^7, 10^7}, {10^6, 9}]; maxBy[list2, 2]; // Timing maxBy[list3, 2]; // Timing maxBy[list9, 2]; // Timing (* output: {0.030341, Null} {0.030912, Null} {0.033313, Null} *) </code></pre> <p>Compared to David's code</p> <pre><code>maxBy[list2, 2]; // Timing maxBy[list3, 2]; // Timing maxBy[list9, 2]; // Timing (* ouput: {0.186175, Null} {0.184989, Null} {0.262018, Null} *) </code></pre> <p>Yoda's code</p> <pre><code>maxBy[list2, 2]; // Timing maxBy[list3, 2]; // Timing maxBy[list9, 2]; // Timing (* ouput: {0.944016, Null} {0.83094, Null} {0.874126, Null} *) </code></pre> <p>And belisarius' code</p> <pre><code>Reverse /@ Take[#, Ordering[#, -1]] &amp;@(Reverse /@ #) &amp;@list2; // Timing Take[#, Ordering[Last /@ #, -1]] &amp;@list2; // Timing #[[Ordering[#, -1, Last@#2 &gt; Last@#1 &amp;]]] &amp;@list2; // Timing #[[First@Position[#, Max@#] &amp;@(Last /@ #)]] &amp;@list2; // Timing (* output: {0.211016, Null} {0.099253, Null} {2.03415, Null} {0.266934, Null} *) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload