# Modifying matrices

### 5.44.6 Modifying matrices

#### Modifying matrix elements by assignment: :=

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

#### Example.

Input:

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

then:

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

Output:

⎡
⎢
⎢
⎣

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.
Input:

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

Output:

⎡
⎢
⎢
⎣

1 | 7 | 6
3 | 4 | 8
1 | 0 | 1

⎤
⎥
⎥
⎦

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:

⎛
⎜
⎜
⎝

0 | 0 | 0
0 | 0 | 0
0 | 0 | 0

⎞
⎟
⎟
⎠

Input:

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

Output:

⎛
⎜
⎜
⎝

1 | 0 | 0
0 | 2 | 0
0 | 0 | 3

⎞
⎟
⎟
⎠

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

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

Output:

⎛
⎜
⎜
⎝

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 4.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:

⎡
⎢
⎣

4 | 5
3 | 6

⎤
⎥
⎦

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:

⎡
⎢
⎣

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

⎡
⎢
⎣

4 | 5
2 | 6

⎤
⎥
⎦

Input:

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

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

will again create two matrices equal to

⎡
⎢
⎣

4 | 5
2 | 6

⎤
⎥
⎦

But:
Input:

A[1] =< [3,7]

will change A to

⎡
⎢
⎣

4 | 5
3 | 7

⎤
⎥
⎦

but B will still be

⎡
⎢
⎣

4 | 5
2 | 6

⎤
⎥
⎦

#### Modifying an element or a row of a matrix: subsop

The subsop command modifies elements of lists (see Section 5.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:

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

#### Examples.

- Input (in Xcas mode):
  subsop([[4,5],[2,6]],[1,0]=3)
  or:
  subsop([[4,5],[2,6]],[1,0],3)
  Output:

  ⎡
  ⎢
  ⎣

  4 | 5
  3 | 6

  ⎤
  ⎥
  ⎦

- Input (in Mupad or TI mode):
  subsop([[4,5],[2,6]],[2,1]=3)
  or:
  subsop([[4,5],[2,6]],[2,1],3)
  Output:

  ⎡
  ⎢
  ⎣

  4 | 5
  3 | 6

  ⎤
  ⎥
  ⎦

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([[4,5],[2,6]],1=[3,3])
  or:
  subsop([[4,5],[2,6]],1,[3,3])
  Output:

  ⎡
  ⎢
  ⎣

  4 | 5
  3 | 3

  ⎤
  ⎥
  ⎦

- Input (in Mupad or TI mode):
  subsop([[4,5],[2,6]],2=[3,3])
  or:
  subsop([[4,5],[2,6]],2,[3,3])
  Output:

  ⎡
  ⎢
  ⎣

  4 | 5
  3 | 3

  ⎤
  ⎥
  ⎦

##### In Maple mode:

Recall that the indexing in Maple mode begins with 1.

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.

#### Example.

Input:

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

Output:

⎡
⎢
⎣

4 | 5
3 | 6

⎤
⎥
⎦

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.

Example:
Input (in Maple mode):

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

Output:

⎡
⎢
⎣

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([[4,5],[2,6]],’1=NULL’)

Output:

⎡
⎣

4 | 5

⎤
⎦

#### Removing rows or columns of a matrix: delrows delcols

The delrows (respectively delcols) command removes one or more rows (respectively 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.

#### Examples.

- Input:
  delrows([[1,2,3],[4,5,6],[7,8,9]],1)
  Output:

  ⎡
  ⎢
  ⎣

  1 | 2 | 3
  7 | 8 | 9

  ⎤
  ⎥
  ⎦

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

  ⎡
  ⎣

  7 | 8 | 9

  ⎤
  ⎦

The delcols command behaves like delrows, but for columns.

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

- Input:
  delcols([[1,2,3],[4,5,6],[7,8,9]],1)
  Output:

  ⎡
  ⎢
  ⎢
  ⎣

  1 | 3
  4 | 6
  7 | 9

  ⎤
  ⎥
  ⎥
  ⎦

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

  ⎡
  ⎢
  ⎢
  ⎣

  3
  6
  9

  ⎤
  ⎥
  ⎥
  ⎦

#### Resizing a matrix or vector: REDIM redim

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

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 0s (if necessary) to make it larger.

#### Examples.

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

  ⎛
  ⎜
  ⎜
  ⎜
  ⎜
  ⎜
  ⎝

  4 | 1 | −2 | 0
  1 | 2 | −1 | 0
  2 | 1 | 0 | 0
  0 | 0 | 0 | 0
  0 | 0 | 0 | 0

  ⎞
  ⎟
  ⎟
  ⎟
  ⎟
  ⎟
  ⎠

- Input:
  REDIM([[4,1,-2],[1,2,-1],[2,1,0]],[2,1])
  Output:

  ⎛
  ⎜
  ⎝

  4
  1

  ⎞
  ⎟
  ⎠

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 0s (if necessary) to make it larger.

#### Examples.

- Input:
  REDIM([4,1,-2,1,2,-1],10)
  Output:

  ⎡ ⎣ | 4,1,−2,1,2,−1,0,0,0,0 | ⎤ ⎦

- Input:
  REDIM([4,1,-2,1,2,-1],3)
  Output:

  ⎡ ⎣ | 4,1,−2 | ⎤ ⎦

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

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

#### Examples.

- Input:
  REPLACE([[1,2,3],[4,5,6]],[0,1],[[5,6],[7,8]])
  Output:

  ⎛
  ⎜
  ⎝

  1 | 5 | 6
  4 | 7 | 8

  ⎞
  ⎟
  ⎠

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

  ⎛
  ⎜
  ⎝

  1 | 2 | 3
  4 | 5 | 7

  ⎞
  ⎟
  ⎠

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.

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

  ⎡ ⎣ | 4,1,10,11,2,−1 | ⎤ ⎦

- Input:
  REPLACE([4,1,-2,1,2,-1],1,[10,11,13])
  Output:

  ⎡ ⎣ | 4,10,11,13,2,−1 | ⎤ ⎦

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

Input:

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

Output:

⎡
⎢
⎣

1 | 4 | 9
16 | 25 | 36

⎤
⎥
⎦