5

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
mrz
  • 11,686
  • 2
  • 25
  • 81

4 Answers4

4

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]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
bill s
  • 68,936
  • 4
  • 101
  • 191
4

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}} *)
Fred Simons
  • 10,181
  • 18
  • 49
  • @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
4

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}}} *)
march
  • 23,399
  • 2
  • 44
  • 100
3

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

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

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

yode
  • 26,686
  • 4
  • 62
  • 167