1
$\begingroup$

I have a matrix that I'd like to augment with identity:

b = {{1, 1, 1, 1}, {1, -1, 1, -1}}.DiagonalMatrix[{1, -1, -1, -1}];
i = DiagonalMatrix[{1, 1}];
b // MatrixForm
i // MatrixForm

i.e.:

blocks

I was able to combine these compatible block matrixes with the following mess:

xx = Flatten[{b, i} // Transpose , 2];
yy = {xx[[1 ;; 6]], xx[[7 ;; 12]]} ;
yy // MatrixForm

augmented

This seems very perverse. I found ArrayFlatten mentioned in https://mathematica.stackexchange.com/a/164897/10, which looked promising, but had trouble adapting it to my simple case.

How would construction of a matrix from blocks such as these be done in a less horrific fashion?

$\endgroup$

1 Answer 1

3
$\begingroup$
Join[b, i, 2]

ArrayFlatten[{{b, i}}]

ArrayPad[b, {{0}, {0, 2}}, i]

MapThread[Join, {b, i}]

all give

 {{1, -1, -1, -1, 1, 0}, {1, 1, -1, 1, 0, 1}}
 TeXForm @ MatrixForm @ %

$$\left( \begin{array}{cccccc} 1 & -1 & -1 & -1 & 1 & 0 \\ 1 & 1 & -1 & 1 & 0 & 1 \\ \end{array} \right)$$

Note: You can use IdentityMatrix[2] for i:

i == IdentityMatrix[2]
 True
$\endgroup$
1
  • 1
    $\begingroup$ Thanks. I needed an extra set of braces in my ArrayFlatten attempt! $\endgroup$ Nov 11, 2020 at 0:47

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.