({{, (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[{{(Subscript[m, 11]),(Subscript[m, 12]),…},{(Subscript[m, 21]),(Subscript[m, 22]),…},…}]
creates a single flattened matrix from a matrix of matrices (Subscript[m, ij]). }, {, (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[a,r]
flattens out r pairs of levels in the array a.}})

OpenersNotesSection(Spacer1)Details

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]) requires that the blocks it flattens have dimensions that fit together.

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]) can be used to form block matrices from arrays of blocks.

For a matrix of matrices, (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[a] yields a matrix whose elements are in the same order as in (RefLink[MatrixForm, paclet:ref/MatrixForm])[a].

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[a] is normally equivalent to (RefLink[Flatten, paclet:ref/Flatten])[a,{{1,3},{2,4}}]. »

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[a,r] is normally equivalent to (RefLink[Flatten, paclet:ref/Flatten])[a,{{1,r+1},{2,r+2},…,{r,2r}}].

For a tensor with rank 2r, (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[a,r] gives a tensor with rank r.

In (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[{{(Subscript[m, 11]),(Subscript[m, 12]),…},{(Subscript[m, 21]),(Subscript[m, 22]),…},…}], all the matrices (Subscript[m, ij]) in the same row must have the same first dimension, and matrices (Subscript[m, ij]) in the same column must have the same second dimension.

In general, in (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])[a,r], all the k(^th) dimensions of a[[(Subscript[i, 1]),(Subscript[i, 2]),…,(Subscript[i, r])]] must be equal for each possible value of (Subscript[i, k]).

Elements at level r whose array depth is less than r are treated as scalars, and are replicated to fill out a rank-r array of the appropriate dimensions.

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects. »

OpenersPrimaryExamplesSection(Spacer1)Examples  (14)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create a block matrix by flattening out a matrix of matrices:

m={{1,2},{3,4}};

ArrayFlatten[{{m,m,m},{m,m,m}}]

Use 0s to represent zero matrices:

m={{1,2},{3,4}};

ArrayFlatten[{{0,0,m},{m,m,0}}]

OpenersExampleSection1(Spacer1)Scope  (4)

Flatten a rank-4 array to rank 2:

ArrayFlatten[Array[a,{1,2,3,4}]]//Dimensions

Flatten only the first four levels of a rank-6 array:

ArrayFlatten[Array[a,{1,2,3,4,5,6}]]//Dimensions

Flatten a rank 6 array to rank 3:

ArrayFlatten[Array[a,{1,2,3,4,5,6}],3]//Dimensions

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects:

s = SparseArray[{i_,i_, j_,k_}->i/(j+k-1),{3,3,2,2}]

m=ArrayFlatten[s]

MatrixForm[m]

Make a sparse matrix from a block matrix of (RefLink[SparseArray, paclet:ref/SparseArray]) objects:

s1=SparseArray[{i_,i_}->i,{2,2}];
s2=SparseArray[{i_,j_}/;Abs[i-j]==1->i-j,{3,3}]

m=ArrayFlatten[{{0,s1},{-s2,0}}]

MatrixForm[m]

OpenersExampleSection2(Spacer1)Applications  (5)

Put together many copies of a "tile":

ArrayPlot[ArrayFlatten[Table[{{2,0,0,1},{1,0,2,1},{0,1,2,2}},{10},{10}]]]

Iterate a 2D substitution system:

NestList[ArrayFlatten[#/. 1->{{1,0},{1,1}}]&,{{1}},3]

ArrayPlot[#,ImageSize->Tiny]&/@%

Iterate a 3D substitution system:

NestList[ArrayFlatten[#/. 1->{{{1,0},{0,0}},{{1,0},{1,1}}},3]&,{{{1}}},3];

Graphics3D[Cuboid/@Position[#,1],ImageSize->Tiny]&/@%

Form an involutory matrix L, such that L.L==1:

involutoryMatrix[A_?MatrixQ,B_]:=Block[{n,k},{n,k}=Dimensions[A];ArrayFlatten[({{IdentityMatrix[k]-B.A, B}, {2 A-A.B.A, A.B-IdentityMatrix[n]}})]/;Dimensions[B]=={k,n}]

L=involutoryMatrix[RandomInteger[{-5,5},{3,2}],RandomInteger[{-5,5},{2,3}]]

Check that the resulting matrix is involutory:

L.L//MatrixForm

Form a block matrix semidiscretization of the wave equation (Subscript[u, tt])==(Subscript[u, xx]) with n spatial points:

n=100;h=1./n;

Differentiation matrix for second-order approximation of (Subscript[u, xx]) with periodic boundary conditions:

dm = SparseArray[{{i_,i_}->-2/h^2,{i_,j_}/;Abs[i-j]==1->1/h^2,{n,1}->1/h^2,{1,n}->1/h^2},{n,n},0.]

Identity matrix of size n:

im = IdentityMatrix[n,SparseArray,WorkingPrecision->MachinePrecision]

Form block matrix a for system U'==a.U where U=={u,(Subscript[u, t])}:

a = ArrayFlatten[{{0.,im},{dm,0.}}]

Identity matrix of size 2 n:

im2 = IdentityMatrix[2 n,SparseArray,WorkingPrecision->MachinePrecision]

Set up an initial condition vector for u(0,x)==(e^(-100 (((x-0.5`))^2))),(Subscript[u, t])(0,x)==0:

ic = Join[Table[Exp[-100(i h - .5)^2],{i,n}], ConstantArray[0.,n]];

Approximate the solution at t==0.25` using the backward Euler method with time step k:

k= h/100;lsf=LinearSolve[im2-k a];solt = ic;Do[solt =lsf[solt],{Round[0.25/k]}];

Show u and (Subscript[u, x]) at t==0.25`:

ListPlot[{Take[solt,n], Take[solt,-n]},DataRange->{0,1},PlotRange->All]

OpenersExampleSection3(Spacer1)Properties & Relations  (3)

(RefLink[MatrixForm, paclet:ref/MatrixForm]) displays matrices of matrices in the same order as (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]):

m={{1,2},{3,4}}; b={{m,m,m},{m,m,m}};

MatrixForm[b]

MatrixForm[ArrayFlatten[b]]

(RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]) is a special case of (RefLink[Flatten, paclet:ref/Flatten]):

m={{1,2},{3,4}}; b={{m,2m},{3m,4m}};

MatrixForm[b]

Flatten[b,{{1,3},{2,4}}]==ArrayFlatten[b]

(RefLink[KroneckerProduct, paclet:ref/KroneckerProduct]) is defined as (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten]) of an (RefLink[Outer, paclet:ref/Outer]) product:

m1=Array[a,{2,2}]; m2=Array[b,{2,2}];

KroneckerProduct[m1,m2]==ArrayFlatten[Outer[Times,m1,m2]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Flatten, paclet:ref/Flatten])  ▪  (RefLink[Join, paclet:ref/Join])  ▪  (RefLink[Band, paclet:ref/Band])  ▪  (RefLink[ArrayQ, paclet:ref/ArrayQ])  ▪  (RefLink[ArrayDepth, paclet:ref/ArrayDepth])  ▪  (RefLink[ArrayPad, paclet:ref/ArrayPad])  ▪  (RefLink[ImageApply, paclet:ref/ImageApply])  ▪  (RefLink[SubstitutionSystem, paclet:ref/SubstitutionSystem])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Rearranging Nested Lists, paclet:tutorial/RearrangingNestedLists])}, {▪(RefLinkPlain[Combining Lists, paclet:tutorial/CombiningLists])}, {▪(RefLinkPlain[Structural Operations, paclet:tutorial/StructuralOperations])}, {▪(RefLinkPlain[Vectors and Matrices, paclet:tutorial/VectorsAndMatrices])}, {▪(RefLinkPlain[Tensors, paclet:tutorial/Tensors])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Constructing Matrices, paclet:guide/ConstructingMatrices])}, {▪(RefLinkPlain[Structure Matrices & Convolution Kernels, paclet:guide/StructureMatricesAndConvolutionKernels])}, {▪(RefLinkPlain[Rearranging & Restructuring Lists, paclet:guide/RearrangingAndRestructuringLists])}, {▪(RefLinkPlain[Computational Systems, paclet:guide/ComputationalSystemsAndDiscovery])}, {▪(RefLinkPlain[Image Filtering & Neighborhood Processing, paclet:guide/ImageFilteringAndNeighborhoodProcessing])}, {▪(RefLinkPlain[Handling Arrays of Data, paclet:guide/HandlingArraysOfData])}, {▪(RefLinkPlain[Basic Image Manipulation, paclet:guide/BasicImageManipulation])}, {▪(RefLinkPlain[Iterated Maps & Fractals, paclet:guide/IteratedMapsAndFractals])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html])(WebLink[: Rearranging Lists, https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html])}})}})