# How to use ArrayFlatten

I'm trying to use the ArrayFlatten command to create an 8 by 8 matrix of zeros from 4 dimensional column vectors as shown below.

s = ConstantArray[0, 4];
{{s, s, s, s, s, s, s, s}, {s, s, s, s, s, s, s, s}} // MatrixForm
ArrayFlatten[{{s, s, s, s, s, s, s, s}, {s, s, s, s, s, s, s,
s}}] // MatrixForm


However, the ArrayFlatten does not do anything here. Please can someone explain what I'm doing wrong and how to fix it? Thanks in advance for any help.

• For new learners visiting the page: ConstantArray[0, {8, 8}] // MatrixForm should generate the desired matrix. This question is about the use of ArrayFlatten command.
– Syed
Sep 14, 2022 at 9:43

You need s to be a matrix before using ArrayFlatten

Help says

creates a single flattened matrix from a matrix of matrices Subscript[m, ij]

But your s was not a matrix.

s = ConstantArray[0, 4]
InputForm[s] To make it a matrix of 4 columns you need to do

s = Transpose[{s}] And now you can use it with ArrayFlatten

s = ConstantArray[0, 4]
s = Transpose[{s}]
(mat = ArrayFlatten[{{s, s, s, s, s, s, s, s}, {s, s, s, s, s, s, s, s}}]) // MatrixForm @Nasser gave the canonical answer and this is more of a general comment, hopefully useful.

Instead of using ArrayFlatten you can consider ArrayReshape

That is

x = ConstantArray[0, 4];
matx = ArrayReshape[Table[x, {i, 1, 8}], {8, 8}];


You can check that

MatrixForm@matx and also

matx === mat


True

with mat being the answer by @Nasser

s = ConstantArray[0, 4];
(t = {s, s, s, s, s, s, s, s}) // MatrixForm


This is a 8x4 matrix, since each s is a row of four elements. Put two of these side by side in a single row, two column arrangement, to get a 8x8 matrix:

ArrayFlatten[{{t, t}}] // MatrixForm


$$\left( \begin{array}{cccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

t = {s, s, s, s}; MatrixForm@t

(ArrayFlatten[{{t, t}, {t, t}}]) // MatrixForm