Modifying matrices

### 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

⎡ ⎢ ⎢ ⎣ |
1 | 5 | 6
3 | 4 | 8
1 | 0 | 1
| ⎤ ⎥ ⎥ ⎦

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

⎡ ⎢ ⎢ ⎣ |
1 | 7 | 6
3 | 4 | 8
1 | 0 | 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]:

M:=matrix(3,3)

⎡ ⎢ ⎢ ⎣ |
0 | 0 | 0
0 | 0 | 0
0 | 0 | 0
| ⎤ ⎥ ⎥ ⎦

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

⎡ ⎢ ⎢ ⎣ |
1 | 0 | 0
0 | 2 | 0
0 | 0 | 3
| ⎤ ⎥ ⎥ ⎦

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

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

⎡ ⎢ ⎢ ⎣ |
1 | 0 | 4
0 | 2 | 5
0 | 0 | 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:

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

⎡ ⎢ ⎣ |
4 | 5
3 | 6
| ⎤ ⎥ ⎦

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]

⎡ ⎢ ⎣ |
4 | 5
3 | 7
| ⎤ ⎥ ⎦

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 [

4 | 5
2 | 6

].

A[1]=<[3,7]

or:

B[1]=<[3,7]

transforms both A and B to [

4 | 5
3 | 7

]. On the other hand, creating A and B with:

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

will again create two matrices equal to [

4 | 5
2 | 6

]. But:

A[1]=<[3,7]

will change A to [

4 | 5
3 | 7

], while B will still be equal to [

4 | 5
2 | 6

].

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

- To modify an element, subsop takes two arguments:
  - A, a matrix.
  - [r,c]=v, an equality between a matrix position (given as a list) and a value.

    The two sides of the equality can also be given as separate 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:
  - A, a matrix.
  - r=L, an equality between a row index and a list with the same length as the rows of A.

    The two sides of the equality can also be given as separate 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:

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

or:

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

⎡ ⎢ ⎣ |
4 | 5
3 | 6
| ⎤ ⎥ ⎦

Input in MuPAD or TI mode:

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

or:

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

⎡ ⎢ ⎣ |
4 | 5
3 | 6
| ⎤ ⎥ ⎦

Input in Xcas mode:

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

or:

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

⎡ ⎢ ⎣ |
4 | 5
3 | 3
| ⎤ ⎥ ⎦

Input in MuPAD or TI mode:

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

or:

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

⎡ ⎢ ⎣ |
4 | 5
3 | 3
| ⎤ ⎥ ⎦

##### In Maple mode:

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

- To modify an element, subsop takes two arguments:
  - [r,c]=v, an equality between a matrix position (given as a list) and a value.

    The two sides of the equality can also be given as separate arguments.

  - A, a matrix.
- 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:

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

⎡ ⎢ ⎣ |
4 | 5
3 | 6
| ⎤ ⎥ ⎦

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

⎡ ⎢ ⎣ |
4 | 5
3 | 3
| ⎤ ⎥ ⎦

##### 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')

⎡ ⎣ |
4 | 5
| ⎤ ⎦

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

⎡ ⎢ ⎣ |
1 | 2 | 3
7 | 8 | 9
| ⎤ ⎥ ⎦

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

⎡ ⎣ |
7 | 8 | 9
| ⎤ ⎦

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

⎡ ⎢ ⎢ ⎣ |
1 | 3
4 | 6
7 | 9
| ⎤ ⎥ ⎥ ⎦

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.

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

⎡ ⎢ ⎣ |
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.

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

⎡ ⎢ ⎣ |
1 | 5 | 6
4 | 7 | 8
| ⎤ ⎥ ⎦

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

⎡ ⎢ ⎣ |
1 | 2 | 3
4 | 5 | 7
| ⎤ ⎥ ⎦

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

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

⎡ ⎢ ⎣ |
1 | 4 | 9
16 | 25 | 36
| ⎤ ⎥ ⎦