Matrix and Vector Entry Assignment
Assignment statements can be used to change the value of entries in an existing Matrix or Vector (whose constructor option ro has not been set to readonly).
If assignment statements are used for this purpose, there are consequences regarding how the resulting Matrix or Vector is returned.
In the following sections, A is a Matrix and V is a Vector.
Assigning Values to Entries
Properties of the Resulting Matrix or Vector
Each of the entry selection formats described in Matrix and Vector Entry Extraction can be used on the left-hand side of an assignment statement. Entries selected in the left-hand side of the assignment statement are replaced by the corresponding entries of the right-hand side. Unspecified entries are set to 0. The number of dimensions in such an assignment statement must match; a Vector can be assigned to a submatrix, provided it is a 1 x n submatrix.
For example, the assignment
A := Matrix([[9,9,9,9],[9,9,9,9],[9,9,9,9],[9,9,9,9]]);
A[1..2, 2..4] := Matrix([[5, 6], [7, 8]]);
replaces A[1, 2] with 5, A[1, 3] with 6, A[2, 2] with 7, and A[2, 3] with 8. The entries A[1, 4] and A[2, 4] are set to 0.
It is not necessary for the selection on the left-hand side of an assignment statement to represent contiguous nor ordered components of the Matrix or Vector. For example, A[[ 3, 1], 1..-1] := B replaces row 3 and row 1 of A with, respectively, row 1 and row 2 of some existing Matrix B.
All of the assignment operations mentioned operate in place -- the resulting Matrix or Vector overwrites the original Matrix or Vector. One consequence is that the command A[1..-1, 1..-1] := Matrix([[...], ..., [...]]) causes a replacement of all the entries of A, whereas the command A := Matrix([[...], ..., [...]]) creates a new Matrix.
For all assignments, the values on the right-hand side must be of the same or compatible datatype as that of the Matrix or Vector on the left-hand side. Furthermore, it is an error if the right-hand side does not fit into the selected submatrix, subvector, or scalar location specified on the left-hand side. It is not an error if the right-hand side is smaller than the left-hand side.
Matrix and Vector Entry Extraction
Download Help Document