2
$\begingroup$

I have a matrix 'mat' and I would like to stack it k times

$$\begin{pmatrix} mat \\ mat \\ mat \\ mat \\ mat \\ \vdots \end{pmatrix}$$

such that the result is also a matrix. I tried Join and ArrayFlatten but it did not seem to work. Especially the fact that k is supposed to stay arbitrary makes it really difficult for me.

$\endgroup$
5
$\begingroup$
mat = Array[a, {3, 3}];

k = 5;
stacked = ArrayFlatten[ConstantArray[{mat}, k]]

Row[MatrixForm /@ {mat, stacked}, Spacer[10]]

enter image description here

Alternatively,

Array[mat &, k, 1, Join] == 
Join @@ ConstantArray[mat, k] ==
Flatten[ConstantArray[mat, k], 1] == 
ArrayFlatten[ConstantArray[mat, k], 1] ==
stacked

True

|improve this answer|||||
$\endgroup$
4
$\begingroup$

Using the same mat and k as those in @kglr 's post, here is another option:

KroneckerProduct[ConstantArray[1, k], mat]
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.