Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After reading some documentations and doing a few experiments, I managed to get a clearer view of this problem.</p> <p>I actually was wondering why <code>Max[]</code> seemed intentionally avoid providing directives that make it return not only the max element itself, but also its position. After all, providing position doesn't change the O(n) complexity of the algorithm. For example, imagine:</p> <pre><code>In[1]:= Max[{991, 993, 992}, ReturnPosition -&gt; True] Out[1]= {2} </code></pre> <p>If that could be done, you can simply use the code below to solve my problem:</p> <pre><code>list[[Max[list[[All, -1]], ReturnPosition -&gt; True]]] </code></pre> <p>But now I realize that the system function <code>Max[]</code> is <em>not</em> designed for finding the max element in <em>lists</em>. You can tell that the Wolfram team obviously made <code>Max[]</code> more like a traditional <code>max</code> function in mathematics ― it does simple symbolic simplifications, it automatically flatten out all lists, it can be in a plotable function, and most importantly, it's <code>Orderless</code>:</p> <pre><code>In[2]:= Attributes[Max] Out[2]= {Flat, NumericFunction, OneIdentity, Orderless, Protected} </code></pre> <p>Which makes positions meaningless. In a word, it treats all the lists inside as mathematical sets.</p> <p>So philosophically it's not trivial for Mathematica to compute this. All I need to do is to "DIY" a function with the O(n) complexity and can do the job. I think TomD is heading the right direction, although I prefer:</p> <pre><code>maxLast[l_] := Cases[l, {___, Max[Last/@l]}] </code></pre> <p>And Heike (黑客?) adopted <code>Pick</code> which may have better techniques especially designed for selecting elements, but there must be no virtual difference in the complexity of the algorithm. And I may rewrite it this way: (fewer names and heads, faster the speed)</p> <pre><code>maxLast[l_] := Pick[l, #, Max[#]] &amp;[Last /@ l] </code></pre> <p>They're both good answers.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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