0

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Strumillo
  • 13
  • 3

3 Answers3

6

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}

rm -rf
  • 88,781
  • 21
  • 293
  • 472
0
a={1,"this"};
b={2,"that"};

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

This gives :

that

andre314
  • 18,474
  • 1
  • 36
  • 69
0
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.

chuy
  • 11,205
  • 28
  • 48