5

We have two numbers: a and b, and $N$ submatrices (here $N=3$ but it should be generalized): m1, m2, m3. The dimension of two sub matrices m1 and m3 are similar and lower than the dimension of m2. For example: m1 and m3 are $r\times r$, but m2 is $s\times s$ and $r<s$.

We wish to construct a bigger matrix whose dimension is d that is equal to $2r+s+2$. (2 for two numbers) in a such way that the numbers and submatrices be on the diagonal of the bigger matrix (as some blocks on the diagonal same as the below schematic picture).

m1 = m3 = {{1, 0, 0, 0}, {0, 1, 0, -1}, {1, 0, -1, 0}, {1, 1, -1, -1}};
m2 = {{-1, 1, 1, 0, 1, 0}, 
      {1, -1, -1, -1, 1, -1}, 
      {-1, 0, 1, 0, -1, 1}, 
      {-1, -1, 1, 1, 1, 0}, 
      {0, -1, 0, 0, -1, -1}, 
      {0, 0, 1, -1, -1, 1}}

The two numbers are $2$ and $-2$. The desired matrix is $16\times16$.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Inzo Babaria
  • 1,513
  • 7
  • 11

3 Answers3

9

Using an undocumented function:

SparseArray`SparseBlockMatrix[{{1, 1} -> {{2}}, {2, 2} -> m1, {3, 3} -> m2,
                               {4, 4} -> m3, {5, 5} -> {{-2}}}]

$$\begin{pmatrix} 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & -1 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & -1 & -1 & -1 & 1 & -1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 0 & 1 & 0 & -1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & -1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & -1 & -1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & -1 & -1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & -1 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -2 \\ \end{pmatrix}$$

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
9

Here is a version that is concise, flexible, and free of undocumented functions:

SparseArray[
  Band[{1, 1}] -> {m1, {{2}}, {{-2}}, m2, m3}
] // MatrixForm

enter image description here

If you want to add more blocks or numbers, just add them to the list, numbers wrapped in {{n}}, as shown above.

JEM_Mosig
  • 3,003
  • 15
  • 28
5

If I understand what you are looking for, the most expeditious way might be to use SparseArray and treat each sub matrix as a Band; you can recover a regular array using Normal. Consider:

MatrixForm@
 SparseArray[{
    {1, 1} -> 2, {-1, -1} -> -2,
    Band[{2, 2}] -> m1, Band[{2, 2} + Dimensions[m1]] -> m2, 
    Band[{2, 2} + Dimensions[m1] + Dimensions[m2]] -> m3
   }, {16, 16}
 ]

Mathematica graphics

MarcoB
  • 67,153
  • 18
  • 91
  • 189