Getting the parts of a matrix

### 13.2.5 Getting the parts of a matrix

##### Accessing rows of a matrix.

The rows of a matrix are the elements of a list, and can be accessed with indices using the postfix […] or the prefix at (see Section 5.1.6). For example:

A:=[[1,2,6],[3,4,8],[1,0,1]]

then:

A[0]

or:

at(A,0)

| ⎡ ⎣ | 1,2,6 | ⎤ ⎦

To extract a column of a matrix, you can first turn the columns into rows with transpose (see Section 14.1.1), then extract the row as above. For example:

tran(A)[1]

or:

at(tran(A),1)

| ⎡ ⎣ | 2,4,0 | ⎤ ⎦

Individual elements are simply elements of the rows. For example:

A[0][1]

2 |  |  |  |  |  |  |  |  |  |

This can be abbreviated by listing the row and column separated by a comma:

A[0,1]

or:

at(A,[0,1])

2 |  |  |  |  |  |  |  |  |  |

The indexing begins with 0; you can have the indices start with 1 by enclosing them in double brackets.

A[[1,2]]

2 |  |  |  |  |  |  |  |  |  |

Use a range (see Section 5.5.1) of indices to get submatrices. Some examples:

A[1,0..2]

| ⎡ ⎣ | 3,4,8 | ⎤ ⎦

A[0..2,1]

| ⎡ ⎣ | 2,4,0 | ⎤ ⎦

A[0..2,1..2]

⎡ ⎢ ⎢ ⎣ |
2 | 6
4 | 8
0 | 1
| ⎤ ⎥ ⎥ ⎦

A[0..1,1..2]

⎡ ⎢ ⎣ |
2 | 6
4 | 8
| ⎤ ⎥ ⎦

This gives you another way to extract a full column, by specifying all the rows as an index interval.

A[0..2,1]

| ⎡ ⎣ | 2,4,0 | ⎤ ⎦

Recall that an index of −1 returns the last element of a list, an index of −2 the second to last element, etc.

A[-1]

| ⎡ ⎣ | 1,0,1 | ⎤ ⎦

A[1,-1]

8 |  |  |  |  |  |  |  |  |  |

##### Extracting rows or columns of a matrix (Maple compatibility).

The row (resp. col) command extracts one or several rows (resp. columns) of a matrix.

- row takes two arguments:
  - A, a matrix.
  - r, a row index or a range n1,…,n2.
- row(A,r) returns the row or sequence of rows given by r.
- col takes two arguments:
  - A, a matrix.
  - c, a column index or a range n1,…,n2.
- col(A,c) returns the column or sequence of columns given by c.

#### Examples

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

| ⎡ ⎣ | 4,5,6 | ⎤ ⎦

row([[1,2,3],[4,5,6],[7,8,9]],0..1)

| ⎡ ⎣ | 1,2,3 | ⎤ ⎦ | , | ⎡ ⎣ | 4,5,6 | ⎤ ⎦

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

| ⎡ ⎣ | 2,5,8 | ⎤ ⎦

col([[1,2,3],[4,5,6],[7,8,9]],0..1)

| ⎡ ⎣ | 1,4,7 | ⎤ ⎦ | , | ⎡ ⎣ | 2,5,8 | ⎤ ⎦

##### Extracting a sub-matrix of a matrix (TI compatibility).

The subMat command finds submatrices of a matrix.

- subMat takes one mandatory argument and four optional arguments:
  - A, a matrix.
  - Optionally, r1, an integer, the row index for the beginning of the submatrix (by default, r1=0).
  - Optionally, c1, an integer, the column index for the beginning of the submatrix (by default, c1=0).
  - Optionally, r2, an integer, the row index for the end of the submatrix (by default, r2 equals one less than the number of rows of A).
  - Optionally, c2, an integer, the column index for the end of the submatrix (by default, c1 equals one less than the number of columns of A).
- subMat(A ⟨,r1,c1,r2,c2⟩) returns the sub-matrix of A from position (r1,c1) to (r2,c2).

#### Examples

A:=[[3,4,5],[1,2,6]]

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

subMat(A,0,1,1,2)

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

subMat(A,0,1,1,1)

⎡ ⎢ ⎣ |
4
2
| ⎤ ⎥ ⎦

subMat(A,1)

or:

subMat(A,1,0)

or:

subMat(A,1,0,1)

or:

subMat(A,1,0,1,2)

⎡ ⎣ |
1 | 2 | 6
| ⎤ ⎦