14.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 6.1.6).
For example:
A:=[[1,2,6],[3,4,8],[1,0,1]] |
then:
or:
To extract a column of a matrix, you can first turn the columns into
rows with transpose (see Section 15.1.1), then
extract the row as above. For example:
or:
Individual elements are simply elements of the rows. For example:
This can be abbreviated by listing the row and column separated by a
comma:
or:
The indexing begins with 0; you can have the indices start with 1
by enclosing them in double brackets.
Use a range (see Section 6.5.1) of indices to get submatrices.
Some examples:
This gives you another way to extract a full column, by specifying all the rows
as an index interval.
Recall that an index of −1 returns the last element of a list, an
index of −2 the second to last element, etc.
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) |
row([[1,2,3],[4,5,6],[7,8,9]],0..1) |
col([[1,2,3],[4,5,6],[7,8,9]],1) |
col([[1,2,3],[4,5,6],[7,8,9]],0..1) |
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
or:
or:
or: