14.2.6 Modifying matrices
Modifying matrix elements by assignment.
You can change the elements of a named matrix by assignment
(see Section 3.3.2).
For example:
A:=[[1,2,6],[3,4,8],[1,0,1]] |
then:
Recall that the elements are indexed starting at 0, using double
brackets allows you to use indices starting at 1.
Use assignment to change several entries of a matrix at one.
For example, create a diagonal matrix with a diagonal of [1,2,3]:
To make the last column [4,5,6]:
Modifying matrix elements by reference.
When you change an element of a matrix with the :=
assignment, a new copy of the matrix is created with the modified
element. Particularly for large matrices, it is more efficient to use
the =< assignment (see Section 3.3.3),
which will change the element of the matrix without making a copy.
For example, enter:
The following commands will all return the matrix A with the
element in the second row, first column, changed to 3.
or:
or:
or:
then:
You can change larger parts of a matrix simultaneously.
For example, the following commands will change
the second row to [3,7]:
or:
or:
or:
The =< assignment must be used carefully, since it not only
modifies a matrix A, it modifies all objects pointing to
A. In a program, initialization should contain a line like
A:=copy(B) (see Section 3.3.4) so modifications done on
A do not affect B, and modifications done on
B do not affect A.
then:
or:
creates two matrices equal to [
].
or:
transforms both A and B to
[
].
On the other hand, creating A and B with:
B:=[[4,5],[2,6]]; A:=copy(B) |
will again create two matrices equal to
[
].
But:
will change A to
[
],
while B will still be equal to
[
].
Modifying an element or a row of a matrix.
The subsop command modifies elements of lists (see
Section 6.3.7), and so use it to modify elements or rows of matrices.
It is used mainly for Maple and MuPAD compatibility,
and the argument list is in a different order in Maple mode. Unlike
:= or =<, it does not require the matrix to be
stored in a variable.
Let A be the matrix given by:
We use this variable in the examples that follow.
(Recall that the indexing in Xcas mode begins with 0, while in
MuPAD and TI modes it begins with 1.)
-
To modify an element, subsop takes two arguments:
- subsop(A,[r,c]=v) returns the matrix which is the
same as A except that the element in row r, column c is now v.
- To modify a row, subsop takes two arguments:
- subsop(A,r=L) returns the matrix which is the
same as A except that row r is now equal to the list L.
Examples
Input in Xcas mode:
or:
Input in MuPAD or TI mode:
or:
Input in Xcas mode:
or:
Input in MuPAD or TI mode:
or:
In Maple mode:
Recall that the indexing in Maple mode is 1-based.
-
To modify an element, subsop takes two arguments:
- subsop([r,c]=v,A) returns the matrix which is the
same as A except that the element in row r, column c is now v.
- To modify a row, subsop takes two arguments:
-
r=L, an equality between a row index and a list with the
same length as the rows of the second argument A.
- A, a matrix.
- subsop(r=L,A) returns the matrix which is the
same as A except that row r is now equal to the list L.
Examples
Input in Maple mode:
In all modes:
If the matrix is stored in a variable, for example with the matrix
A as above, it is easier to enter A[1,0]:=3 and
A[1]=[3,3] to modify A as above.
Also, note that subsop with a ’n=NULL’ argument
deletes row number n.
Example
Input in Xcas mode:
Removing rows or columns of a matrix.
The delrows
(resp. delcols) command removes
one or more rows (resp. columns) from a matrix.
-
delrows takes two arguments:
-
A, a matrix.
- r, an integer or a range of integers.
- delrows(A,r) returns the matrix equal to A with
the row(s) given by r removed.
- delcols takes two arguments:
-
A, a matrix.
- c, an integer or a range of integers.
- delrows(A,c) returns the matrix equal to A with
the column(s) given by c removed.
Examples
delrows([[1,2,3],[4,5,6],[7,8,9]],1) |
delrows([[1,2,3],[4,5,6],[7,8,9]],0..1) |
delcols([[1,2,3],[4,5,6],[7,8,9]],1) |
delcols([[1,2,3],[4,5,6],[7,8,9]],0..1) |
Resizing a matrix or vector.
The REDIM or
redim
command resizes matrices and vectors.
-
For matrices, REDIM takes two arguments:
-
A, a matrix.
- [m,n], a list of two positive integers.
- REDIM(A,[m,n]) returns A resized to an m×
n matrix, removing elements (if necessary) to make it smaller and
adding zeros (if necessary) to make it larger.
- For vectors, REDIM takes two arguments:
-
L, a list.
- n, a positive integer.
- REDIM(L,n) returns L resized to a list of length
n, removing elements (if necessary) to make it smaller and
adding zeros (if necessary) to make it larger.
Examples
REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[5,4]) |
|
| ⎡
⎢
⎢
⎢
⎢
⎢
⎣ | 4 | 1 | −2 | 0 |
1 | 2 | −1 | 0 |
2 | 1 | 0 | 0 |
0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 |
| ⎤
⎥
⎥
⎥
⎥
⎥
⎦ |
|
| | | | | | | | | | |
|
REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[2,1]) |
REDIM([4,1,-2,1,2,-1],10) |
|
| ⎡
⎣ | 4,1,−2,1,2,−1,0,0,0,0 | ⎤
⎦ |
| | | | | | | | | | |
|
Replacing part of a matrix or vector.
The REPLACE
or replace
command replaces part of a matrix or vector.
-
For matrices, REPLACE takes three arguments:
-
A, a matrix.
- [m,n], a list of two positive integers.
- B, a matrix.
REPLACE(A,[m,n],B) returns the matrix equal to A
but with the upper left corner of B placed at row m, column n,
replacing the previous elements of A. The matrix B will be
shrunk, if necessary, to fit.
- For lists, REPLACE takes three arguments:
-
L, a list.
- n, a positive integer.
- M, another list.
REPLACE(L,n,M) returns the list equal to L
but with the elements beginning at index n replaced by the
elements of M, replacing the previous elements of L. The list
M will be shrunk, if necessary, to fit.
Examples
REPLACE([[1,2,3],[4,5,6]],[0,1],[[5,6],[7,8]]) |
REPLACE([[1,2,3],[4,5,6]],[1,2],[[7,8],[9,0]]) |
REPLACE([4,1,-2,1,2,-1],2,[10,11]) |
REPLACE([4,1,-2,1,2,-1],1,[10,11,13]) |
Applying a function to the elements of a matrix.
The apply command can apply a function to the elements of a
matrix. (See Section 6.3.28 for other uses of apply.)
-
apply takes three arguments:
-
f, a function of one variable.
- A, a matrix.
- matrix, the symbol.
apply(f,A,matrix) returns a matrix whose elements are
f(x) for the elements x of A.
Example
apply(x->x^2,[[1,2,3],[4,5,6]],matrix) |