Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

On the mathematica reference website it gives examples using the MaximalBy function, However when I am using it, I see no desired output. This leads me to wonder if mathematica has stopped using this function.

example:

MaximalBy[{{25, 16}, {1, 50}, {2, 8}, {13, 93}, {41, 21}}, Last]

and i get:

MaximalBy[{{25, 16}, {1, 50}, {2, 8}, {13, 93}, {41, 21}}, Last]

when I should get:

{13,93}

Since this does not work, how can I use RankedMax to get this desired result

share|improve this question

closed as off-topic by Artes, RunnyKine, Öskå, Mr.Wizard Aug 27 at 22:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Artes, RunnyKine, Öskå, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

    
I get {{13, 93}} - "10.0 for Microsoft Windows (64-bit) (June 29, 2014)" –  eldo Aug 27 at 21:07
    
I have mathematica 9, Windows 64 bit, I will restart mathematica –  Crisp Aug 27 at 21:08
    
mmmh dont know whats the problem will try somethings else –  Crisp Aug 27 at 21:15
3  
MaximalBy is new in v10. –  evanb Aug 27 at 21:24
1  

2 Answers 2

up vote 4 down vote accepted

MaximalBywas only introduced with V10

For V9 and prior use

Last @ SortBy[{{25, 16}, {1, 50}, {2, 8}, {13, 93}, {41, 21}}, Last]

{13, 93}

share|improve this answer
    
Mhhhh why did you put "Last" in Last @ SortBy, there is already a Last at the end of the List which tell the program which sublist to pick out. –  Crisp Aug 27 at 21:35
    
@Crisp Because omitting the Last as per your comment would return the whole sorted list, i.e. it wouldn't replicate MaximalBy. –  eldo Aug 27 at 21:42
1  
@eldo This does not quite replicate MaximalBy, which will return a list of all elements that are maximal under the function. You could use Last@SortBy[GatherBy[list, Last], #[[1, -1]] &]; you could wrap that in Take to support MaximalBy's 3rd argument. –  mfvonh Aug 27 at 22:02
    
@mfvonh Thanks - I believe you're right. Let me try it tomorrow or - even better - publish your own answer :) –  eldo Aug 27 at 22:07

To expand @eldo's answer a bit, you can fully replicate MaximalBy like this:

Clear@myMaximalBy;
myMaximalBy[e_List, f_, n_: All] := Take[Last@SortBy[GatherBy[e, f], #[[1, -1]] &], n];

Then

SeedRandom@1;
list1 = {{25, 16}, {1, 50}, {2, 8}, {13, 93}, {41, 21}};
list2 = RandomInteger[10, {100, 2}];

myMaximalBy[list1, Last]

{{13, 93}}

myMaximalBy[list2, Last]

{{8, 10}, {0, 10}, {0, 10}, {4, 10}, {5, 10}, {9, 10}, {5, 10}}

myMaximalBy[list2, Last, 3]

{{8, 10}, {0, 10}, {0, 10}}

The only difference is that this method has been expanded to return multiple matches if appropriate.

share|improve this answer
    
I agree - but I think that subtle differences like this are confusing - even for experienced users. –  eldo Aug 27 at 22:29
    
@eldo Certainly. Your answer was the meat of it; I just figured it was worth hammering out the details. –  mfvonh Aug 27 at 22:41
    
This looks good, thanks for this code however I will have to take some time to understand it at my level thanks again. –  Crisp Aug 27 at 22:45
    
After you look it over, if there are points that are not clear to you feel free to ask. The easiest way is to drop into the chatroom. Just use @mfvonh to get my attention. –  mfvonh Aug 27 at 22:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.