# How to eliminate { } in an arbitrary order in subgroup of a nested matrix?

How can reach the second matrix from the first nested one:

first={{{{0, 1}, {1, 0}}, {{1, 0}, {0, 1}}}, {{{1, 0}, {0, 1}}, {{0, 1}, {1,
0}}}};
second={{0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}};


One way to do this is with ArrayFlatten:

ArrayFlatten[first]
{{0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}}


Two approaches I can think of:

Partition[Flatten[first], 4]
Flatten[first, {{1}, {2}, {3, 4}}]

ArrayReshape[first, {4, 4}]


{{0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}}

Also

Module[{x = #}, x[[All, 0]] = Sequence; x[[All, All, All, 0]] = Sequence; x] &@first
MapAt[Sequence &, first, {{All, 0}, {All, All, All, 0}}]
ReplacePart[first, {{_, 0} -> Sequence, {_, _, _, 0} :> Sequence}]

first=
{{{{0, 1}, {1, 0}}, {{1, 0}, {0, 1}}}, {{{1, 0}, {0, 1}}, {{0, 1}, {1,0}}}};

Join @@@ Join @@ first


{{0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}}

first = {{{{0, 1}, {1, 0}}, {{1, 0}, {0, 1}}}, {{{1, 0}, {0, 1}}, {{0,1}, {1, 0}}}};

second = {{0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}};


Using ReplaceAll and Catenate:

Catenate@(first /. m_?MatrixQ :> Join @@ m) === second

(*True*)


Or using ReplaceAll and Cases:

Cases[first /. m_?MatrixQ :> Join @@ m, v_ /; VectorQ[v], 2] === second

(*True*)

Clear["Global*"];
first = {{{{0, 1}, {1, 0}}, {{1, 0}, {0, 1}}}, {{{1, 0}, {0, 1}}, {{0,
1}, {1, 0}}}};

rule = {{a_, b_}, {c_, d_}} :> {a, b, c, d}

res1 = first /. rule /. rule


pos = Position[first, _?MatrixQ]
res2 = Sequence @@@ MapAt[Flatten, first, pos]


MatrixForm /@ {first, res1, res2}
`