Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

enter image description here

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

enter image description here

 {{{{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
share|improve this question
    
Please post code instead of images so we have something to work with. –  mfvonh Jun 25 at 18:53
    
Also, how exactly is this supposed to work? Are the differences always going to be simply nonzero vs zero? –  mfvonh Jun 25 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. –  mostafa Jun 25 at 19:00
    
@Kuba, I am so sorry, I could not understand your question. –  mostafa Jun 25 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. –  mostafa Jun 25 at 19:12

2 Answers 2

up vote 7 down vote accepted

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)$

share|improve this answer
    
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}}}} –  mostafa Jun 25 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; ... –  mfvonh Jun 25 at 19:55
1  
@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 Jun 25 at 19:59
    
@Kuba Gotcha, I was just trying to clarify for mostafa , not to correct your answer :) –  mfvonh Jun 25 at 20:03
1  
I independently arrived at almost exactly the same solution so I appended it to this answer. –  Mr.Wizard Jun 26 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)$

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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