5
$\begingroup$

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}};
$\endgroup$

6 Answers 6

5
$\begingroup$

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}}
$\endgroup$
4
$\begingroup$

Two approaches I can think of:

Partition[Flatten[first], 4]
Flatten[first, {{1}, {2}, {3, 4}}]
$\endgroup$
4
$\begingroup$
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}]
$\endgroup$
1
$\begingroup$
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}}

$\endgroup$
1
$\begingroup$
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*)
$\endgroup$
1
$\begingroup$
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}

enter image description here

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