0
$\begingroup$

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??

$\endgroup$
  • $\begingroup$ a = {1, "this"}; b = {2, "that"}; #[[Ordering[#, -1, #[[1]] &]]] &@{a, b}? $\endgroup$ – Dr. belisarius Dec 6 '13 at 20:43
  • $\begingroup$ Shorter a = {1, "this"}; b = {2, "that"}; #[[Ordering[#, -1]]] &@{a, b} $\endgroup$ – Dr. belisarius Dec 6 '13 at 21:25
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Dec 6 '13 at 21:27
  • $\begingroup$ Mods should have a way to vote normally $\endgroup$ – Dr. belisarius Dec 6 '13 at 21:34
  • $\begingroup$ @belisarius If you disagree with the closure please vote to reopen. $\endgroup$ – Mr.Wizard Dec 7 '13 at 1:37
6
$\begingroup$

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}

$\endgroup$
  • 4
    $\begingroup$ Show a screen capture, or it didn't happen $\endgroup$ – Dr. belisarius Dec 6 '13 at 21:15
  • $\begingroup$ @belisarius Now vote! :D $\endgroup$ – rm -rf Dec 6 '13 at 21:16
  • $\begingroup$ Ok, Ok, you have it :) $\endgroup$ – Dr. belisarius Dec 6 '13 at 21:19
0
$\begingroup$
a={1,"this"};
b={2,"that"};

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

This gives :

that

$\endgroup$
  • $\begingroup$ wooooooow, thank you! $\endgroup$ – Strumillo Dec 6 '13 at 20:57
  • $\begingroup$ can I ask one more thing? how do I get whole b back? I'm just mesmerized by 3 @@@ and how it works:) $\endgroup$ – Strumillo Dec 6 '13 at 20:59
  • $\begingroup$ To obtain {2,"that"} instead of "that", you can do Max[0, a[[1]], b[[1]]] /. ((#[[1]] -> #) & /@ {a, b}) $\endgroup$ – andre314 Dec 6 '13 at 21:04
0
$\begingroup$
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.

$\endgroup$

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