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}}
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityOne 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]
Max
Listable
in the way Leonid did in this answer.
$\endgroup$
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}} *)
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}}} *)
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}}
Max[]
can do it, but it needs some assistance:MapThread[Max, {{{1, 2}, {3, 4}} , {{5, 1}, {7, 2}}}, 2]
. $\endgroup$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$