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

4 Answers 4

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$
1
  • 1
    $\begingroup$ Also @FredSimons: I think it is much cleaner to make Max Listable in the way Leonid did in this answer. $\endgroup$
    – Karsten7
    Oct 27, 2015 at 14:21
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$
1
  • $\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$ Oct 27, 2015 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$
2
  • $\begingroup$ Upvoted, of course. :) $\endgroup$ Oct 27, 2015 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, 2015 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.