For named matrices, the elements can be changed by assignment.
Recall the elements are indexed starting at 0, using double brackets
allows you to use indices starting at 1.
Input:
Output:
Input:
then:
Output:
Input:
then:
Output:
When an element of a matrix is changed 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, which will change the element of the
matrix without making a copy. For example, defining A as
Input:
the following commands will all return the matrix A with the
element in the second row, first column, changed to 3.
Input:
or:
or:
or:
then:
Output:
Larger parts of a matrix can be changed simultaneously. Letting
A := [[4,5],[2,6]] again, the following commands will change
the second row to [3,7]
Input:
or:
or:
or:
The =< assignment must be used carefully, since it not only
modifies a matrix A, it modifies all objects pointing to
the matrix. In a program, initialization should contain a line like
A := copy(B), so modifications done on A don’t
affect B, and modifications done on B don’t affect
A. For example,
Input:
then:
or
Input:
creates two matrices equal to [[4,5],[2,6]].
Then
Input:
or:
will transform both A and B to [[4,5],[3,7]].
On the other hand, creating A and B with
Input:
will again create two matrices equal to [[4,5],[2,6]]. But
Input:
will change A to [[4,5],[3,7]], but B will still be [[4,5],[2,6]].