TransWikia.com

Concatenate matrices in a specific way

Mathematica Asked by xabdax on March 3, 2021

Is there an elegant way to start with a matrix of the form

$$
A = (B,C)
$$

where B and C have dimensions n x m, and construct a large matrix that looks like

$$
begin{pmatrix}
B & C & mathbf{0} & mathbf{0}&ddots&mathbf{0}
mathbf{0} & B & C & mathbf{0}&ddots&mathbf{0}
ddots &ddots &ddots &ddots &ddots&mathbf{0}
mathbf{0} &mathbf{0} &mathbf{0} &mathbf{0} & mathbf{0} & B
end{pmatrix}
$$

where $mathbf{0}$ is the zero matrix with the same dimensions as B and C? I would like to keep the size of this matrix variable.

2 Answers

dims = {3, 2};
{bB, cC} = Array[#, dims] & /@ {b, c};

Row[MatrixForm /@ {bB, cC}, Spacer[10]]

enter image description here

k = 4;
kdims = k dims;
SparseArray[{Band[{1, 1}, kdims] -> {bB}, 
   Band[{1, dims[[2]] + 1}, kdims] -> {cC}}, kdims] // MatrixForm

enter image description here

Correct answer by kglr on March 3, 2021

build[mB_, mC_, nZs_] := With[{
   m = Length@mB,
   n = Length@mB[[1]]},
  Module[{base, len},
   len = n*(2 + nZs);
   base = PadRight[ArrayFlatten[{{mB, mC}}], {m, len}, 0];
   Catenate@Table[PadLeft[base[[All,;; n*(2 + nZs - i)]], {m, len}, 0], {i,0, nZs + 1}]
   ]]

For example:

m = 3
n = 2
mB = RandomInteger[100, {m, n}]
mC = RandomInteger[100, {m, n}]
nZs = 3
build[mB, mC, nZs] // MatrixForm

Answered by Alan on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP