Previous Up Next

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:

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



156
348
101



          

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

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



176
348
101



          

Use assignment to change several entries of a matrix at one. For example, create a diagonal matrix with a diagonal of [1,2,3]:

M:=matrix(3,3)
     



000
000
000



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



100
020
003



          

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

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



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

For example, enter:

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.

A[1,0]:=3

or:

A[1,0]=<3

or:

A[[2,1]]:=3

or:

A[[2,1]]=<3

then:

A
     


45
36


          

You can change larger parts of a matrix simultaneously. For example, the following commands will change the second row to [3,7]:

A[1]:=[3,7]

or:

A[1]=<[3,7]

or:

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

or:

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


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 3.3.4) so modifications done on A do not affect B, and modifications done on B do not affect A.

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

then:

A:=B

or:

A=<B

creates two matrices equal to [

45
26

].

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:

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

will again create two matrices equal to [

45
26

]. But:

A[1]=<[3,7]

will change A to [

45
37

], while B will still be equal to [

45
26

].

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:

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

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

Examples

Input in Xcas mode:

subsop(A,[1,0]=3)

or:

subsop(A,[1,0],3)
     


45
36


          

Input in MuPAD or TI mode:

subsop(A,[2,1]=3)

or:

subsop(A,[2,1],3)
     


45
36


          

Input in Xcas mode:

subsop(A,1=[3,3])

or:

subsop(A,1,[3,3])
     


45
33


          

Input in MuPAD or TI mode:

subsop(A,2=[3,3])

or:

subsop(A,2,[3,3])
     


45
33


          
In Maple mode:

Recall that the indexing in Maple mode is 1-based.

Examples

Input in Maple mode:

subsop([2,1]=3,A)
     


45
36


          
subsop(2=[3,3],A)
     


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(A,'1=NULL')
     

45

          
Removing rows or columns of a matrix.

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

Examples

delrows([[1,2,3],[4,5,6],[7,8,9]],1)
     


123
789


          
delrows([[1,2,3],[4,5,6],[7,8,9]],0..1)
     

789

          
delcols([[1,2,3],[4,5,6],[7,8,9]],1)
     



13
46
79



          
delcols([[1,2,3],[4,5,6],[7,8,9]],0..1)
     



3
6
9



          
Resizing a matrix or vector.

The REDIM or redim command resizes matrices and vectors.

Examples

REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[5,4])
     






41−20
12−10
2100
0000
0000






          
REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[2,1])
     


4
1


          
REDIM([4,1,-2,1,2,-1],10)
     

4,1,−2,1,2,−1,0,0,0,0
          
REDIM([4,1,-2,1,2,-1],3)
     

4,1,−2
          
Replacing part of a matrix or vector.

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

Examples

REPLACE([[1,2,3],[4,5,6]],[0,1],[[5,6],[7,8]])
     


156
478


          
REPLACE([[1,2,3],[4,5,6]],[1,2],[[7,8],[9,0]])
     


123
457


          
REPLACE([4,1,-2,1,2,-1],2,[10,11])
     

4,1,10,11,2,−1
          
REPLACE([4,1,-2,1,2,-1],1,[10,11,13])
     

4,10,11,13,2,−1
          
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.)

Example

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


149
162536


          

Previous Up Next