3
$\begingroup$

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

$\endgroup$

2 Answers 2

3
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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}} *)
$\endgroup$
1
  • $\begingroup$ IMHO this is the correct answer. +1 $\endgroup$
    – Mr.Wizard
    Mar 16, 2014 at 13:54

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.