5

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
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Unbelievable
  • 4,847
  • 1
  • 20
  • 46

2 Answers2

7

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 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}}}} – Unbelievable Jun 25 '14 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 '14 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 '14 at 19:59
  • @Kuba Gotcha, I was just trying to clarify for mostafa , not to correct your answer :) – mfvonh Jun 25 '14 at 20:03
  • @mfvonh right, I probably should've explained that earlier so thanks :) – Kuba Jun 25 '14 at 20:05
  • 1
    I independently arrived at almost exactly the same solution so I appended it to this answer. – Mr.Wizard Jun 26 '14 at 05:48
  • @Kuba, No why?! I am practicing this one, again, for not forgetting which I have learn of you. – Unbelievable Jul 01 '14 at 11:46
  • I am worried if other users who had tried to answer my question be unhappy if I accept just one answer while their answers are completely correct too. – Unbelievable Jul 01 '14 at 12:00
  • @mostafa no, of course not, you should accept the answer that fits your need the best. which you are using or you just want :) Upvotes are indicators of correctness. – Kuba Jul 01 '14 at 12:01
  • @mostafa take a look at meta there are couple of related topics. p.s. could you please delete old not relevant comments? – Kuba Jul 01 '14 at 12:02
  • I deleted that were related to me. Ok I am going to take a look. – Unbelievable Jul 01 '14 at 12:14
4

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371