Home page for accesible maths 2 Combining Vectors

Style control - access keys in brackets

Font (2 3) - + Letter spacing (4 5) - + Word spacing (6 7) - + Line spacing (8 9) - +

2.2 Constructing a diagonal matrix

In some cases we may want to construct a diagonal matrix. The function diag is available for this. diag may be used in several ways. If a single scalar input n is supplied then the n×n identity matrix 𝐈n is created. For instance

I <- diag(10)

creates a 10×10 identity matrix. If two scalar inputs k and n are supplied then the n×n matrix k𝐈n is created, e.g.

I <- diag(5,7)

creates a 7×7 matrix with diagonal elements 5. If a single vector input is supplied then a diagonal matrix with diagonal elements corresponding to the elements of that vector is created e.g.

H <- diag(c(1,2,3,4))

creates the matrix

[1000020000300004].

In the next section we will see that diag can also be used to extract the diagonal elements of a matrix.

 

Workshop 1: Constructing a matrix (I)

Construct a matrix

[1-10310-10121101-102111-10002]

 

 

Workshop 2: Constructing a matrix (II)

Construct a 10×10 matrix of the form:

𝐕=[1-1-200-11-1-2-2-100-2-2-1-100-2-11]

 

 

Quiz 1: Constructing a matrix (III)

Which of the following commands creates the matrix

[21732476252035506582338536811264156711429445974]
  1. (A)

    matrix((2:74),5,5,byrow=TRUE)

  2. (B)

    matrix(3*(1:25) - 1, 5,5,byrow=TRUE)

  3. (C)

    matrix(rep(2:74,3),5,5,byrow=TRUE)

  4. (D)

    matrix(rep(c(2,17,32,47,62),5),5,5,byrow=FALSE)

  5. (E)

    matrix(seq(2,74,by=3),5,5,byrow=FALSE)