# Comparison Operation for Nested Matrices

I have a nested matrix n as bellow

n = {{a, b}, {c, d}}
a = {{0, t, q, dh}, {0, 1, 0, th}, {1, 0, 0, sh}}
b = {{0, t, q, dh}, {0, 0, 0, th}, {0, 0, 1, sh}}
c = {{0, t, q, dh}, {1, 0, 1, th}, {0, 0, 0, sh}}
d = {{0, t, q, dh}, {0, 1, 0, th}, {1, 0, 0, sh}}


containing letters and numbers. I am going to do a particular operation in each row yielding the result shown bellow:

 {{{{0, t, q, dh}, {0, 1, 0, th}, {1, 0, 1, sh}}},
{{{0, t, q, dh}, {1, 1, 1, th}, {1, 0, 0, sh}}}}M

• Please post code instead of images so we have something to work with. Commented Jun 25, 2014 at 18:53
• Also, how exactly is this supposed to work? Are the differences always going to be simply nonzero vs zero? Commented Jun 25, 2014 at 18:55
• in sub matrices in each row, and for corresponding elemenets in each of these sub matrices, the comparison between 0 and 0 is 0, between 0 and 1, yields 1. and between 1 and 1 results 1. and letters such as t, q, dh and so on must be repeated. Commented Jun 25, 2014 at 19:00
• @Kuba, I am so sorry, I could not understand your question. Commented Jun 25, 2014 at 19:04
• in 'n', as n = {{a, b}, {c, d}}, which is written above a11 must be comprised with b11, a12 (that is 't' will must be comprise just for b12 (that is same as a12), the correspoding elements: a12,b12.........a13,b13........a31,b31. and so on. Commented Jun 25, 2014 at 19:12

I don't know what would be a general pattern but for this case you can use:

{MapThread[Max, #, 2]} & /@ n

{{
{{0, t, q, dh},
{0, 1, 0, th},
{1, 0, 1, sh}}},
{{{0, t, q, dh},
{1, 1, 1, th},
{1, 0, 0, sh}}}}


Alternatively:

List /@ MapThread[Max, n, 3] // MatrixForm


$\left( \begin{array}{c} \left( \begin{array}{cccc} 0 & t & q & \text{dh} \\ 1 & 1 & 1 & \text{th} \\ 1 & 0 & 0 & \text{sh} \end{array} \right) \\ \left( \begin{array}{cccc} 0 & t & q & \text{dh} \\ 0 & 1 & 0 & \text{th} \\ 1 & 0 & 1 & \text{sh} \end{array} \right) \end{array} \right)$

• Your result is : {{{0, t, q, dh}, {0, 1, 0, th}, {1, 0, 1, sh}}, {{0, t, q, dh}, {1, 1, 1, th}, {1, 0, 0, sh}}} ,while my desire is: {{{{0, t, q, dh}, {0, 1, 0, th}, {1, 0, 1, sh}}}, {{{0, t, q, dh}, {1, 1, 1, th}, {1, 0, 0, sh}}}} Commented Jun 25, 2014 at 19:40
• It can't as far as I know -- I don't think that is actually happening with these particular matrices. But you could use Block[{Max},Unprotect@Max;Max[s_Symbol,0]:=s; ... Commented Jun 25, 2014 at 19:55
• @mfvonh It does not matter since there will not be a case of two different symbols. Or a case of symbol and 0. That's why I've asked many questions before answering.
– Kuba
Commented Jun 25, 2014 at 19:59
• @Kuba Gotcha, I was just trying to clarify for mostafa , not to correct your answer :) Commented Jun 25, 2014 at 20:03
• I independently arrived at almost exactly the same solution so I appended it to this answer. Commented Jun 26, 2014 at 5:48

If only Max were Listable:

listMax = Function[, Max[##], Listable];


Then:

List /@ listMax @@@ n // MatrixForm


$\left( \begin{array}{c} \left( \begin{array}{cccc} 0 & t & q & \text{dh} \\ 1 & 1 & 1 & \text{th} \\ 1 & 0 & 0 & \text{sh} \end{array} \right) \\ \left( \begin{array}{cccc} 0 & t & q & \text{dh} \\ 0 & 1 & 0 & \text{th} \\ 1 & 0 & 1 & \text{sh} \end{array} \right) \end{array} \right)$