In Mathematica, Max[]
is the most efficient function to get the maximum number in a list of numbers, but how do I find the list with the maximum last element in a list of lists? e.g. the 2-d coordinate with the biggest x
part in a series of coordinates.
My best try is SortBy
, but obviously I don't need the program to sort my list, only the maximum one I need.
How about this function (defined here only for 2D lists):
Example:
Perhaps:
Exploiting the fact that
Ordering[ ]
orders lists by their first elementEdit
Or much better (I think):
Edit
Also:
Edit
Perhaps faster:
Not the most efficient but simpler?
or
Usage
Output:
After reading some documentations and doing a few experiments, I managed to get a clearer view of this problem.
I actually was wondering why
Max[]
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:If that could be done, you can simply use the code below to solve my problem:
But now I realize that the system function
Max[]
is not designed for finding the max element in lists. You can tell that the Wolfram team obviously madeMax[]
more like a traditionalmax
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'sOrderless
:Which makes positions meaningless. In a word, it treats all the lists inside as mathematical sets.
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:
And Heike (黑客?) adopted
Pick
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)They're both good answers.
Here's an approach that relies on
Transpose
:For example: list = {{4, 3}, {5, 10}, {20, 1}, {3, 7}};
It can handle more than two elements per sublist, provided that the sublists are all the same length.
Of course, the results will vary according to the random numbers you have generated.
Edit
Here's some timing data for large lists.
SortBy
is clearly slower. but doesn't seem as influenced by the number of elements in each sublist. First, mymaxBy
code followed bySortBy
:Using the same list2, here's some timing data for Yoda's code. Although his routine is also called maxBy, it is his that produced the output that follows:
Again, with the same list2, some data for Belisarius' code:
His second suggestion is the fastest of all tested.
Here is my approach using
Pick
This version works on any number of elements per sublist provided
n
is less than or equal to the length of the shortest sublist.Timings for this version on my machine
Compared to David's code
Yoda's code
And belisarius' code