# Element-wise minimum of two matrices

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

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

## 1 Answer

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