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.

This question already has an answer here:

So I have this question, using a toy example:

a={1,"this"};
b={2,"that"};

result=Max[0,a[[1]],b[[1]]]

the result will be 2. However what I want to obtain is "that" (or at least "b"). How do I do this without making dozens of "If"s and conditionals??

share|improve this question

marked as duplicate by Mr.Wizard Dec 6 '13 at 21:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
a = {1, "this"}; b = {2, "that"}; #[[Ordering[#, -1, #[[1]] &]]] &@{a, b}? –  belisarius Dec 6 '13 at 20:43
    
thank you so much, pretty nice one! –  Strumillo Dec 6 '13 at 21:19
    
Shorter a = {1, "this"}; b = {2, "that"}; #[[Ordering[#, -1]]] &@{a, b} –  belisarius Dec 6 '13 at 21:25
    
I have marked this as a duplicate. I feel that the subject is already well covered there. (And several other places.) Please review the answers there; if afterward you disagree vote or flag to reopen. –  Mr.Wizard Dec 6 '13 at 21:27
    
Mods should have a way to vote normally –  belisarius Dec 6 '13 at 21:34

3 Answers 3

If you have Mathematica on the Raspberry Pi (or Mathematica 10), you could use Association to store your data, which allows you to do these operations easily:

With[{a = <|1 -> "this", 2 -> "that"|>},
    a@Max@Keys@a
]
(* that *)

You can convert your list to an association as: Association @@ Rule @@@ {a, b}

share|improve this answer
4  
Show a screen capture, or it didn't happen –  belisarius Dec 6 '13 at 21:15
    
@belisarius Now vote! :D –  rm -rf Dec 6 '13 at 21:16
    
Ok, Ok, you have it :) –  belisarius Dec 6 '13 at 21:19
a={1,"this"};
b={2,"that"};

Max[0, a[[1]], b[[1]]] /. Rule @@@ {a, b}

This gives :

that

share|improve this answer
    
wooooooow, thank you! –  Strumillo Dec 6 '13 at 20:57
    
can I ask one more thing? how do I get whole b back? I'm just mesmerized by 3 @@@ and how it works:) –  Strumillo Dec 6 '13 at 20:59
    
To obtain {2,"that"} instead of "that", you can do Max[0, a[[1]], b[[1]]] /. ((#[[1]] -> #) & /@ {a, b}) –  andre Dec 6 '13 at 21:04
a={1,"this"};
b={2,"that"};

Cases[{a, b}, {key_, value_} /; (key == Max[{a, b}[[All, 1]]])]

or

Select[{a, b}, (#[[1]] == Max[{a, b}[[All, 1]]] &)]

For variety's sake. Likely not the way to go for long lists though.

share|improve this answer

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