11
$\begingroup$

I have four matrices

m = {{M11, M12, M13}, {M21, M22, M23}, {M31, M32, M33}};
n = {{N11, N12, N13}, {N21, N22, N23}, {N31, N32, N33}};
p = {{P11, P12, P13}, {P21, P22, P23}, {P31, P32, P33}};
q = {{Q11, Q12, Q13}, {Q21, Q22, Q23}, {Q31, Q32, Q33}};

and i want to construct the following

ArrayFlatten[Table[{{m[[k1, k2]], n[[k1, k2]]}, {p[[k1, k2]], q[[k1, k2]]}}, {k1,1, 3}, {k2, 1, 3}]]

 {{M11, N11, M12, N12, M13, N13},
  {P11, Q11, P12, Q12, P13, Q13},
  {M21, N21, M22, N22, M23, N23},
  {P21, Q21, P22, Q22, P23, Q23},
  {M31, N31, M32, N32, M33, N33},
  {P31, Q31, P32, Q32, P33, Q33}}

There is a more elegant and rapid way to this thing?

$\endgroup$
2
  • 3
    $\begingroup$ ArrayFlatten@Transpose[{{m, n}, {p, q}}, {3, 4, 1, 2}] $\endgroup$ Commented Jan 20, 2017 at 23:29
  • $\begingroup$ Of course, because of the relationship between Flatten[] and Transpose[], Carl's and Simon's snippets are equivalent. $\endgroup$ Commented Jan 21, 2017 at 7:20

2 Answers 2

14
$\begingroup$

I would use Flatten:

m = {{M11, M12, M13}, {M21, M22, M23}, {M31, M32, M33}};
n = {{N11, N12, N13}, {N21, N22, N23}, {N31, N32, N33}};
p = {{P11, P12, P13}, {P21, P22, P23}, {P31, P32, P33}};
q = {{Q11, Q12, Q13}, {Q21, Q22, Q23}, {Q31, Q32, Q33}};

Flatten[{{m, n}, {p, q}}, {{3, 1}, {4, 2}}] //TeXForm

$\begin{pmatrix} \text{M11} & \text{N11} & \text{M12} & \text{N12} & \text{M13} & \text{N13} \\ \text{P11} & \text{Q11} & \text{P12} & \text{Q12} & \text{P13} & \text{Q13} \\ \text{M21} & \text{N21} & \text{M22} & \text{N22} & \text{M23} & \text{N23} \\ \text{P21} & \text{Q21} & \text{P22} & \text{Q22} & \text{P23} & \text{Q23} \\ \text{M31} & \text{N31} & \text{M32} & \text{N32} & \text{M33} & \text{N33} \\ \text{P31} & \text{Q31} & \text{P32} & \text{Q32} & \text{P33} & \text{Q33} \\ \end{pmatrix}$

$\endgroup$
4
  • $\begingroup$ Ah ok, I did not know this use the Flatten function!! Thanks $\endgroup$
    – Kowalski
    Commented Jan 20, 2017 at 23:41
  • 1
    $\begingroup$ @Kowalski here are some explanations about the generalized form of Flatten, though personally, I have still difficulties to find the right index ordering. $\endgroup$
    – andre314
    Commented Jan 21, 2017 at 20:53
  • $\begingroup$ @Carl can you also explain how you determine the indices for Flatten? $\endgroup$
    – Ali Hashmi
    Commented Jun 5, 2017 at 21:16
  • 1
    $\begingroup$ I just experiment. $\endgroup$
    – Carl Woll
    Commented Jun 5, 2017 at 21:25
3
$\begingroup$
♮ = ## & @@@ (#) &@(## & @@@ (#) & /@ (#) & /@ {##}) &;

Mathematica graphics

♮[{m, n}, {p, q}] // MatrixForm

Mathematica graphics

$\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.