# Greater operator should give greater element [duplicate]

4>3 gives True.

What is the "greater_operator" to obtain the result 4?

For lists accordingly:

{{1, 2}, {3, 4}} "greater_operator" {{5, 1}, {7, 2}}

should give a resulting list: {{5, 2}, {7, 4}}

• Max[] can do it, but it needs some assistance: MapThread[Max, {{{1, 2}, {3, 4}} , {{5, 1}, {7, 2}}}, 2]. – J. M.'s ennui Oct 26 '15 at 22:11
• ++1 if you wouldt turn this into an answer – eldo Oct 26 '15 at 22:18
• (I've capped hours ago; if anyone wants to answer with that, you have my permission.) The fully general version goes something like p1 = {{{9, 6}, {-7, 4}}, {{-5, 9}, {8, 2}}}; p2 = {{{3, -9}, {-9, -4}}, {{-7, 3}, {8, 8}}}; MapThread[Max, {p1, p2}, ArrayDepth[p1]]. – J. M.'s ennui Oct 26 '15 at 22:30
• Related: (3217), (23395), (95666) – Mr.Wizard Jul 7 '16 at 21:18

One way to get the desired behavior is to make the Max function Listable:

Unprotect[Max];
SetAttributes[Max, Listable];
lst1 = {{1, 2}, {3, 4}}; lst2 = {{5, 1}, {7, 2}};
Max[lst1, lst2]

{{5, 2}, {7, 4}}


You could also do the same thing a bit more safely by changing the Max function attributes only when needed. For instance:

max[list1_, list2_] := Module[{out}, Unprotect[Max]; SetAttributes[Max, Listable];
out = Max[list1, list2]; ClearAttributes[Max, Listable]; out]

• Also @FredSimons: I think it is much cleaner to make Max Listable in the way Leonid did in this answer. – Karsten 7. Oct 27 '15 at 14:21

A slight improvement of the answer of bill s, without unprotecting Max:

max[list1_, list2_] := Block[{Max}, Attributes[Max] = {Listable}; Max[list1, list2]]

max[{{1, 2}, {3, 4}}, {{5, 1}, {7, 2}}]

(* {{5, 2}, {7, 4}} *)

• @Karsten. Unfortunately, I missed that contribution of Leonid, something that is always highly regretable. Thank you for calling my attention to it. Indeed, the solution then becomes surprisingly simple: max=Function[Null, Max[##], Listable]. – Fred Simons Oct 27 '15 at 17:45

Here is J.M.'s direct solution:

MapThread[Max, {{{1, 2}, {3, 4}} , {{5, 1}, {7, 2}}}, 2]


Here is J.M.'s more general solution:

p1 = {{{9, 6}, {-7, 4}}, {{-5, 9}, {8, 2}}};
p2 = {{{3, -9}, {-9, -4}}, {{-7, 3}, {8, 8}}};
MapThread[Max, {p1, p2}, ArrayDepth[p1]]


Although not the question, it was referenced at the beginning of the post, so here's how you could apply > instead of Max:

MapThread[#1 > #2 &, {p1, p2}, ArrayDepth[p1]]
(* {{{True, True}, {True, True}}, {{True, True}, {False, False}}} *)

• Upvoted, of course. :) – J. M.'s ennui Oct 27 '15 at 16:22
• @J.M. Oh, thanks! I worked hard on this one. :) I waited the requisite amount of time, I feel, and since no one else took advantage of your kind offer, I figured I would! – march Oct 27 '15 at 16:23

Suppose all your digtal is non-negtive,I give a undocumental function for this

lst1 = {{1, 2}, {3, 4}};
lst2 = {{5, 1}, {7, 2}};
InternalMaxAbs[lst1, lst2]
`

{{5,2},{7,4}}