Combining matrices

### 14.2.3 Combining matrices

##### Making a matrix with a list of matrices.

The blockmatrix command combines several matrices into one larger matrix.

- blockmatrix takes three arguments:
  - m and n, two positive integers.
  - L, a list of m n matrices such that the first m matrices have the same number of rows; the next m matrices have the same number of rows, etc; and the number of columns in each group of m matrices is the same (for example, all the matrices in L could have the same dimension), so that the n groups of m matrices can be stacked above each other to form a larger matrix.
- blockmatrix(m,n,L) returns the larger matrix formed by the matrices in L by putting each group of m matrices next to each other, and stacking the resulting n matrices on top of each other.

#### Examples

blockmatrix(2,3,[idn(2),idn(2),idn(2),idn(2),idn(2),idn(2)])

⎡ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 1 | 0 | 1 | 0
0 | 1 | 0 | 1 | 0 | 1
1 | 0 | 1 | 0 | 1 | 0
0 | 1 | 0 | 1 | 0 | 1
| ⎤ ⎥ ⎥ ⎥ ⎦

blockmatrix(3,2,[idn(2),idn(2),idn(2),idn(2),idn(2),idn(2)])

⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 1 | 0
0 | 1 | 0 | 1
1 | 0 | 1 | 0
0 | 1 | 0 | 1
1 | 0 | 1 | 0
0 | 1 | 0 | 1
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

blockmatrix(2,2,[idn(2),newMat(2,3),newMat(3,2),idn(3)])

⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 | 0 | 0
0 | 1 | 0 | 0 | 0
0 | 0 | 1 | 0 | 0
0 | 0 | 0 | 1 | 0
0 | 0 | 0 | 0 | 1
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

blockmatrix(3,2,[idn(1),newMat(1,4),newMat(2,3),idn(2),newMat(1,2),[[1,1,1]]])

⎡ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 | 0 | 0
0 | 0 | 0 | 1 | 0
0 | 0 | 0 | 0 | 1
0 | 0 | 1 | 1 | 1
| ⎤ ⎥ ⎥ ⎥ ⎦

A:=[[1,1],[1,1]];B:=[[1],[1]]:; blockmatrix(2,3,[2*A,3*A,4*A,5*B,newMat(2,4),6*B])

⎡ ⎢ ⎢ ⎢ ⎣ |
2 | 2 | 3 | 3 | 4 | 4
2 | 2 | 3 | 3 | 4 | 4
5 | 0 | 0 | 0 | 0 | 6
5 | 0 | 0 | 0 | 0 | 6
| ⎤ ⎥ ⎥ ⎥ ⎦

##### Making a matrix by concatenating columns of two matrices.

The semi_augment command concatenates two matrices with the same number of columns.

- semi_augment takes two arguments: A and B, two matrices with the same number of columns.
- semi_augment(A,B) returns the matrix which has the rows of A followed by the rows of B.

#### Examples

semi_augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])

⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
3 | 4
2 | 1
0 | 1
1 | 2
4 | 5
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

semi_augment([[3,4,2]],[[1,2,4]])

⎡ ⎢ ⎣ |
3 | 4 | 2
1 | 2 | 4
| ⎤ ⎥ ⎦

Note the difference with concat.

concat([[3,4,2]],[[1,2,4]]

⎡ ⎣ |
3 | 4 | 2 | 1 | 2 | 4
| ⎤ ⎦

Indeed, when the two matrices A and B have the same dimension, concat makes a matrix with the same number of rows as A and B by gluing them side by side.

concat([[3,4],[2,1],[0,1]],[[1,2],[4,5]]

⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
3 | 4
2 | 1
0 | 1
1 | 2
4 | 5
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

But input:

concat([[3,4],[2,1]],[[1,2],[4,5]]

⎡ ⎢ ⎣ |
3 | 4 | 1 | 2
2 | 1 | 4 | 5
| ⎤ ⎥ ⎦

##### Making a matrix by gluing two matrices together.

The augment or concat command glues two matrices, either side by side or one on top of the other.

- augment has two arguments: A and B, two matrices with the same number of rows or the same number of columns.
- augment(A,B) returns the matrix consisting of:
  - if A and B have the same number of rows, then the matrix being returned consists of the columns of A followed by the columns of B; in other words, A and B are glued side by side.
  - if A and B do not have the same number of rows but have the same number of columns, then the matrix being returned consists of the rows of A followed by the rows of B; in other words, A and B are glued one on top of the other.

Note that if A and B have the same dimension, then augment(A,B) will return a matrix with the same number of rows as A and B by horizontal gluing. In that case, if you want to combine them by vertical gluing, you must use semi_augment(A,B).

#### Examples

augment([[3,4,5],[2,1,0]],[[1,2],[4,5]])

⎡ ⎢ ⎣ |
3 | 4 | 5 | 1 | 2
2 | 1 | 0 | 4 | 5
| ⎤ ⎥ ⎦

augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])

⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
3 | 4
2 | 1
0 | 1
1 | 2
4 | 5
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

augment([[3,4,2]],[[1,2,4]]

⎡ ⎣ |
3 | 4 | 2 | 1 | 2 | 4
| ⎤ ⎦

##### Appending a column to a matrix.

The border command adds a column to a matrix.

- border takes two arguments:
  - A, a matrix.
  - b, a list whose length equals the number of rows of A.
- border(A,L) returns a matrix equal to A with the transpose of L forming an additional column to the right. Equivalent commands are tran([op(tran(A)),b]) and tran(append(tran(A),b)).

#### Examples

border([[1,2,4],[3,4,5]],[6,7])

⎡ ⎢ ⎣ |
1 | 2 | 4 | 6
3 | 4 | 5 | 7
| ⎤ ⎥ ⎦

border([[1,2,3,4],[4,5,6,8],[7,8,9,10]],[1,3,5])

⎡ ⎢ ⎢ ⎣ |
1 | 2 | 3 | 4 | 1
4 | 5 | 6 | 8 | 3
7 | 8 | 9 | 10 | 5
| ⎤ ⎥ ⎥ ⎦