5
$\begingroup$

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

2 Answers 2

7
$\begingroup$

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

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

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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