# Combining matrices

### 5.44.3 Combining matrices

#### Making a matrix with a list of matrices: blockmatrix

The blockmatrix 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.

  If the matrices in L each have the same dimension p × q, the result will be a matrix with dimension p*n × q*m.

#### Examples.

- Input:
  blockmatrix(2,3,[idn(2),idn(2),idn(2), idn(2),idn(2),idn(2)])
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎣

  1 | 0 | 1 | 0 | 1 | 0
  0 | 1 | 0 | 1 | 0 | 1
  1 | 0 | 1 | 0 | 1 | 0
  0 | 1 | 0 | 1 | 0 | 1

  ⎤
  ⎥
  ⎥
  ⎥
  ⎦

- Input:
  blockmatrix(3,2,[idn(2),idn(2), idn(2),idn(2),idn(2),idn(2)])
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎢
  ⎢
  ⎢
  ⎣

  1 | 0 | 1 | 0
  0 | 1 | 0 | 1
  1 | 0 | 1 | 0
  0 | 1 | 0 | 1
  1 | 0 | 1 | 0
  0 | 1 | 0 | 1

  ⎤
  ⎥
  ⎥
  ⎥
  ⎥
  ⎥
  ⎥
  ⎦

- Input:
  blockmatrix(2,2,[idn(2),newMat(2,3), newMat(3,2),idn(3)])
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎢
  ⎢
  ⎣

  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

  ⎤
  ⎥
  ⎥
  ⎥
  ⎥
  ⎥
  ⎦

- Input:
  blockmatrix(3,2,[idn(1),newMat(1,4), newMat(2,3),idn(2),newMat(1,2),[[1,1,1]]])
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎣

  1 | 0 | 0 | 0 | 0
  0 | 0 | 0 | 1 | 0
  0 | 0 | 0 | 0 | 1
  0 | 0 | 1 | 1 | 1

  ⎤
  ⎥
  ⎥
  ⎥
  ⎦

  Input:

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

  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎣

  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 from two matrices: semi_augment

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.

- Input:
  semi_augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎢
  ⎢
  ⎣

  3 | 4
  2 | 1
  0 | 1
  1 | 2
  4 | 5

  ⎤
  ⎥
  ⎥
  ⎥
  ⎥
  ⎥
  ⎦

  [[3,4],[2,1],[0,1],[1,2],[4,5]]
- Input:
  semi_augment([[3,4,2]],[[1,2,4]])
  Output:

  ⎡
  ⎢
  ⎣

  3 | 4 | 2
  1 | 2 | 4

  ⎤
  ⎥
  ⎦

  Note the difference with concat.
  Input:
  concat([[3,4,2]],[[1,2,4]]
  Output:

  ⎡
  ⎣

  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.
  Input:
  concat([[3,4],[2,1],[0,1]],[[1,2],[4,5]]
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎢
  ⎢
  ⎣

  3 | 4
  2 | 1
  0 | 1
  1 | 2
  4 | 5

  ⎤
  ⎥
  ⎥
  ⎥
  ⎥
  ⎥
  ⎦

  but input:
  concat([[3,4],[2,1]],[[1,2],[4,5]]
  Output:

  ⎡
  ⎢
  ⎣

  3 | 4 | 1 | 2
  2 | 1 | 4 | 5

  ⎤
  ⎥
  ⎦

#### Making a matrix from two matrices: augment concat

The augment command glues two matrices, either side by side or one on top of the other.
Here, concat can be used as a synonym for augment.

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

#### Examples.

- Input:
  augment([[3,4,5],[2,1,0]],[[1,2],[4,5]])
  Output:

  ⎡
  ⎢
  ⎣

  3 | 4 | 5 | 1 | 2
  2 | 1 | 0 | 4 | 5

  ⎤
  ⎥
  ⎦

- Input:
  augment([[3,4],[2,1],[0,1]],[[1,2],[4,5]])
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎢
  ⎢
  ⎣

  3 | 4
  2 | 1
  0 | 1
  1 | 2
  4 | 5

  ⎤
  ⎥
  ⎥
  ⎥
  ⎥
  ⎥
  ⎦

- Input:
  augment([[3,4,2]],[[1,2,4]]
  Output:

  ⎡
  ⎣

  3 | 4 | 2 | 1 | 2 | 4

  ⎤
  ⎦

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

#### Appending a column to a matrix: border

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; so
  border(A,b)=tran([op(tran(A)),b])=tran(append(tran(A),b))

#### Examples.

- Input:
  border([[1,2,4],[3,4,5]],[6,7])
  Output:

  ⎡
  ⎢
  ⎣

  1 | 2 | 4 | 6
  3 | 4 | 5 | 7

  ⎤
  ⎥
  ⎦

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

  ⎡
  ⎢
  ⎢
  ⎣

  1 | 2 | 3 | 4 | 1
  4 | 5 | 6 | 8 | 3
  7 | 8 | 9 | 10 | 5

  ⎤
  ⎥
  ⎥
  ⎦