# Getting the parts of a matrix

### 5.44.5 Getting the parts of a matrix

#### Accessing parts of a matrix: [] at

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

#### Example.

Input:

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

then:

A[0]

or:

at(A,0)

Output:

⎡ ⎣ | 1,2,6 | ⎤ ⎦

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

#### Example.

Input:

tran(A)[1]

or:

at(tran(A),1)

Output:

⎡ ⎣ | 2,4,0 | ⎤ ⎦

Individual elements are simply elements of the rows.

#### Example.

Input:

A[0][1]

Output:

2

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

A[0,1]

or:

at(A,[0,1])

Output:

2

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

A[[1,2]]

Output:

2

You can use a range (see Section 5.37.1) of indices to get submatrices.

#### Examples.

- Input:
  A[1,0..2]
  Output:

  ⎡ ⎣ | 3,4,8 | ⎤ ⎦

- Input:
  A[0..2,1]
  Output:

  ⎡ ⎣ | 2,4,0 | ⎤ ⎦

- Input:
  A[0..2,1..2]
  Output:

  ⎡
  ⎢
  ⎢
  ⎣

  2 | 6
  4 | 8
  0 | 1

  ⎤
  ⎥
  ⎥
  ⎦

- Input:
  A[0..1,1..2]
  Output:

  ⎡
  ⎢
  ⎣

  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]
  Output:

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

#### Examples.

- Input:
  A[-1]
  Output:

  ⎡ ⎣ | 1,0,1 | ⎤ ⎦

- Input:
  A[1,-1]
  Output:

  8

#### Extracting rows or columns of a matrix (Maple compatibility): row col

The row (respectively col) command extracts one or several rows (respectively 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.

#### Examples.

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

  ⎡ ⎣ | 4,5,6 | ⎤ ⎦

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

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

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

#### Examples.

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

  ⎡ ⎣ | 2,5,8 | ⎤ ⎦

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

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

#### Extracting a sub-matrix of a matrix (TI compatibility): subMat

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

#### Example.

Input:

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

Output:

⎡
⎢
⎣

3 | 4 | 5
1 | 2 | 6

⎤
⎥
⎦

- Input:
  subMat(A,0,1,1,2)
  Output:

  ⎡
  ⎢
  ⎣

  4 | 5
  2 | 6

  ⎤
  ⎥
  ⎦

- Input:
  subMat(A,0,1,1,1)
  Output:

  ⎡
  ⎢
  ⎣

  4
  2

  ⎤
  ⎥
  ⎦

  [[4],[2]]
- Input:
  subMat(A,1)
  or:
  subMat(A,1,0)
  or:
  subMat(A,1,0,1)
  or:
  subMat(A,1,0,1,2)
  Output:

  ⎡
  ⎣

  1 | 2 | 6

  ⎤
  ⎦