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 this multidimensional matrix:

{{{{a1, a2}, {a3, a4}}, {{b1, b2}, {b3, b4}}}, {{{c1, c2}, {c3, c4}}, {{d1, d2}, {d3, d4}}}}

I would like to convert it into this regular matrix:

{{a1, a2, b1, b2}, {a3, a4, b3, b4}, {c1, c2, d1, d2}, {c3, c4, d3, d4}}

I have tried to use Flatten and Partition, but then I get this result:

{{a1, a2, a3, a4}, {b1, b2, b3, b4}, {c1, c2, c3, c4}, {d1, d2, d3, d4}}

Thanks in advance :-)

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Here is what you need:

Flatten[
   {
      {{{a1, a2}, {a3, a4}}, {{b1, b2}, {b3, b4}}}, 
      {{{c1, c2}, {c3, c4}}, {{d1, d2}, {d3, d4}}}
   }
   , 
   {{1, 3}, {2, 4}}
]

Have a look here for an explanation.

share|improve this answer

You can use ArrayFlatten for this:

ArrayFlatten[{{{{a1, a2}, {a3, a4}}, {{b1, b2}, {b3, b4}}},
              {{{c1, c2}, {c3, c4}}, {{d1, d2}, {d3, d4}}}}]

(* {{a1, a2, b1, b2}, {a3, a4, b3, b4}, {c1, c2, d1, d2}, {c3, c4, d3, d4}} *)
share|improve this answer
    
IMHO this is the correct answer. +1 –  Mr.Wizard Mar 16 at 13:54

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.