1
$\begingroup$

I am trying to compare two identically-shaped matrices and select the minimum of the two. This is an extension of a past question: Element-wise max of two lists, except I have three nested lists.

Data1 = {{{3, 2}, {3, 4}, {4, 3}}, {{5, 2}, {3, 6}, {2, 4}}, {{13, 
2}, {34, 62}, {24, 14}}, {{2, 3}, {5, 8}, {3, 2}}}
Data2 = {{{2, 3}, {2, 14}, {2, 1}}, {{6, 4}, {2, 7}, {2, 4}}, {{13, 
2}, {34, 62}, {24, 14}}, {{2, 3}, {5, 8}, {3, 2}}}

My desired output is:

{{{2, 2}, {2, 4}, {2, 1}}, {{5, 2}, {2, 6}, {2, 4}}, {{13, 2}, {34, 62}, {24, 14}}, {{2, 3}, {5, 8}, {3, 2}}}

$\endgroup$
  • 4
    $\begingroup$ You say maximum, your example appears to be minimum. MapThread[Min, {Data1, Data2}, 3] Don't use uppercase initials on your symbols, bad idea. $\endgroup$ – ciao Oct 23 at 4:55
  • $\begingroup$ Thanks @ciao I have adjusted the text to be consistent with minimum $\endgroup$ – Mike Major Oct 23 at 6:08
2
$\begingroup$

Since $min(x,y) = x - (x - y)^+$ (where $a^+ = max(a,0)$), you can use:

Data1 - Ramp[Data1 - Data2]

{{{2, 2}, {2, 4}, {2, 1}}, {{5, 2}, {2, 6}, {2, 4}}, {{13, 2}, {34, 62}, {24, 14}}, {{2, 3}, {5, 8}, {3, 2}}}

or

Subtract[Data1, Ramp @ Subtract[Data1, Data2]]

{{{2, 2}, {2, 4}, {2, 1}}, {{5, 2}, {2, 6}, {2, 4}}, {{13, 2}, {34, 62}, {24, 14}}, {{2, 3}, {5, 8}, {3, 2}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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