Previous Up Next

6.44.6  Modifying matrices

Modifying matrix elements by assignment: :=

You can change the elements of a named matrix by assignment (see Section 5.4.2).


Example.
Input:

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

then:

A[0,1]:= 5:;
A

Output:




156
348
101



Recall that the elements are indexed starting at 0, using double brackets allows you to use indices starting at 1.
Input:

A[[1,2]]:=7:;
A

Output:




176
348
101




You can use assignment to change several entries of a matrix at one.


Example.
Create a diagonal matrix with a diagonal of [1,2,3]:
Input:

M:= matrix(3,3)

Output:




000
000
000



Input:

M[0..2,0..2]:= [1,2,3]

Output:




100
020
003



To make the last column [4,5,6]:
Input:

M[0..2,2]:= [4,5,6]

Output:




104
025
006



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 5.4.3), which will change the element of the matrix without making a copy.


Example.
Input:

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

The following commands will all return the matrix A with the element in the second row, first column, changed to 3.
Input:

A[1,0]:= 3

or:

A[1,0] =< 3

or:

A[[2,1]]:= 3

or:

A[[2,1]] =< 3

then:

A

Output:



45
36


You can change larger parts of a matrix simultaneously.


Example.
Input:

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

The following commands will change the second row to [3,7]
Input:

A[1]:= [3,7]

or:

A[1] =< [3,7]

or:

A[[2]]:= [3,7]

or:

A[[2]] =< [3,7]

Output:



45
37


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 5.4.4) so modifications done on A don’t affect B, and modifications done on B don’t affect A.

For example:
Input:

B:= [[4,5],[2,6]]

then:

A:= B

or:

A =< B

creates two matrices equal to



45
26


Input:

A[1] =< [3,7]

or:

B[1] =< [3,7]

transforms both A and B to



45
37


On the other hand, creating A and B with:
Input:

B:= [[4,5],[2,6]]
A:= copy(B)

will again create two matrices equal to



45
26


But:
Input:

A[1] =< [3,7]

will change A to



45
37


but B will still be



45
26


Modifying an element or a row of a matrix: subsop

The subsop command modifies elements of lists (see Section 6.40.7), and so you can 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 give by:
Input:

A:=[[4,5],[2,6]]
In Xcas, Mupad and TI modes:

Recall that the indexing in Xcas mode begins with 0, while in Mupad and TI modes it begins with 1.

To modify an element:


Examples.


To modify a row:


Examples.

In Maple mode:

Recall that the indexing in Maple mode begins with 1.

To modify an element:


Example.
Input:

subsop([2,1]=3,[[4,5],[2,6]])

Output:



45
36



To modify a row:


Example:
Input (in Maple mode):

subsop(2=[3,3],[[4,5],[2,6]])

Output:



45
33


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

subsop([[4,5],[2,6]],’1=NULL’)

Output:


45

Removing rows or columns of a matrix: delrows delcols

The delrows (respectively delcols) command removes one or more rows (respectively columns) from a matrix.


Examples.


The delcols command behaves like delrows, but for columns.


Examples.

Resizing a matrix or vector: REDIM redim

The REDIM command resizes matrices and vectors.
redim is a synonym for REDIM.

For matrices:


Examples.


For vectors:


Examples.

Replacing part of a matrix or vector: REPLACE replace

The REPLACE command replaces part of a matrix or vector.
replace is a synonym for REPLACE.

For matrices:


Examples.


For lists:


Examples.

Applying a function to the elements of a matrix: apply

The apply command can apply a function to the elements of a matrix. (See Section 6.40.28 for other uses of apply.)


Example.
Input:

apply(x->x^2,[[1,2,3],[4,5,6]],matrix)

Output:



149
162536



Previous Up Next