Switch the screen on/off Go back to last page Go forward one page Find out more details about the advertisement
John Moores University. Main CWIS Site
  

 Back  

               Try some examples

Updating elements of vectors and matrices

The elements of a vector or matrix can be changed or updated using the SUB infix operator in conjunction with the operators :=, :+, :-, :* and :/

 If the vector v is defined as [a,b,c,d,e], simplifying v SUB 4:=z will exchange the fourth element of v with z.

v := [a, b, c, d, e] , and v4  := z

v = [a, b, c, z, e]

Note that just entering v SUB 4:=z into the algebra window will not make the exchange!  It must be simplified.  This simplification is performed automatically if the assignment is within a program.  

If M is matrix defined on the previous page, simplifying M SUB 3 SUB 4:=2 will exchange the element in row 3 column 4 with the number 2.

M3,4    := 2

Similarly, the update operators can be used to change elements of vectors and matrices.  For example doubling row 1 column 1 we would use

 M1,1 :* 2

  

To subtract 2 from row 3 column 1 we would use

M3,1 :- 2

 

MAP(u,x,c) and MAP(u,k,m,n,s)

If u is and expression involving the variable x and c is a set or vector, the function MAP(u,x,c) evaluates u(x) for x equal to each element of c.  Similarly, MAP(u,k,m,n,s) evaluates u(k) for k=m to n in steps of s.    s defaults to 1 if omitted from the function.

This is a powerful function that can perform many tasks within a program. If the MAP function is used in the Derive algebra window it just returns the value true.  At first it may seem a bit of a pointless function but you will see the usefulness of it in this next example.

Using the matrix M as in previous example

MAP(Mi,i := i, i, 1, 4)                

Simplifying we get true.  So what?  Well, all the assignments

M1,1 := 1 ,  M2,2 := 2 ,  M3,3 := 3 and  M4,4 := 4

have been made to M, as we can see below.

DIM(A)or DIMENSION(A)

This function returns the number of elements in a vector or the number of rows in a matrix. DIM(A SUB 1) will return the number of columns of a matrix.

We now have the tools to write programs in which we can manipulate vectors and matrices.  The program below takes any matrix and replaces all the elements below the leading diagonal with 0.

FEE(M, rows, columns) := PROG(rows := DIM(M), columns := DIM(M™1), MAP(MAP(M™(c + r)™c := 0, r, 1, rows - 1), c, 1, columns - 1), M)

which displays as

FEE(M, rows, columns) :=
    Prog 

       
rows := DIM(M)
       
columns := DIM(M™1)
       
MAP(MAP(M™(c + r)™c := 0, r, 1, rows - 1), c, 1, columns - 1)
       M

Again, using the matrix,  M,  as an example:

and