This question already has an answer here:

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

marked as duplicate by MarcoB, Community Aug 6 '17 at 16:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Inzo, out of curiosity, why did you revert my clean-up edits? Had I mistakenly mis-represented your question? – MarcoB Aug 6 '17 at 2:29
  • So sorry, I did not revert intentionally your editing. I was adding somethings in the question and I could not understand what I should did correctly. Please take your editing my question to be pretended better – Inzo Babaria Aug 6 '17 at 2:34
  • So sorry for that. I pressed a key incorrectly. It caused your editing was not applied – Inzo Babaria Aug 6 '17 at 2:35
  • possible duplicate q/a: How to form a block-diagonal matrix from a list of matrices? – kglr Aug 6 '17 at 3:23
up vote 7 down vote accepted

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}$$

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.

  • If you're going to put two scalars next to each other on a diagonal band, you might as well do Band[{1, 1}] -> {m1, DiagonalMatrix[{2, -2}], m2, m3} – J. M. is computer-less Aug 6 '17 at 22:35
  • @J.M.: True, but this way it is clear that the scalars can be put anywhere in the sequence. – JEM_Mosig Aug 6 '17 at 22:47

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

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