5
$\begingroup$

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.

$\endgroup$
1
  • 2
    $\begingroup$ 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. $\endgroup$
    – Syed
    Commented Sep 14, 2022 at 9:43

3 Answers 3

7
$\begingroup$

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]

Mathematica graphics

To make it a matrix of 4 columns you need to do

s = Transpose[{s}]

Mathematica graphics

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

Mathematica graphics

$\endgroup$
7
$\begingroup$

@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];
x = Thread@{x};
matx = ArrayReshape[Table[x, {i, 1, 8}], {8, 8}];

You can check that

MatrixForm@matx

matx

and also

matx === mat

True

with mat being the answer by @Nasser

$\endgroup$
5
$\begingroup$
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)$$


Or start with a block that is a 4x4 matrix:

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

and make two columns and two rows from these sub-elements.

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

to get the same 8x8 matrix as above.

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