5
$\begingroup$

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

$\endgroup$

marked as duplicate by C. E., Mr.Wizard Jul 7 '16 at 21:18

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.

  • 4
    $\begingroup$ Max[] can do it, but it needs some assistance: MapThread[Max, {{{1, 2}, {3, 4}} , {{5, 1}, {7, 2}}}, 2]. $\endgroup$ – J. M. is away Oct 26 '15 at 22:11
  • 1
    $\begingroup$ ++1 if you wouldt turn this into an answer $\endgroup$ – eldo Oct 26 '15 at 22:18
  • 2
    $\begingroup$ (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]]. $\endgroup$ – J. M. is away Oct 26 '15 at 22:30
  • $\begingroup$ Related: (3217), (23395), (95666) $\endgroup$ – Mr.Wizard Jul 7 '16 at 21:18
4
$\begingroup$

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]
$\endgroup$
4
$\begingroup$

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}} *)
$\endgroup$
  • $\begingroup$ @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]. $\endgroup$ – Fred Simons Oct 27 '15 at 17:45
4
$\begingroup$

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}}} *)
$\endgroup$
  • $\begingroup$ Upvoted, of course. :) $\endgroup$ – J. M. is away Oct 27 '15 at 16:22
  • $\begingroup$ @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! $\endgroup$ – march Oct 27 '15 at 16:23
3
$\begingroup$

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

$\endgroup$

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