Home page for accesible maths LAB100

Style control - access keys in brackets

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

1 Extracting Data from a Matrix

In workshop week 6 you were introduced to subsetting a vector using the square brackets [ ]. We also use this technique to extract information from a matrix, but we need to provide the row number and the column number of the matrix element that we are interested in. In general, to extract the value contained in the mth row and nth column cell of matrix F, we would execute the command:

F[ m, n ]

Using the matrices that you have created in this workshop, execute the following extraction commands. Remember that the row number is first and the column number is second.

A[ 3, 3 ]

B[ 2, 1 ]

D[ 4, 2 ]

E[ 1, 3 ]

Note that the number you put into the square brackets cannot be greater than the dimension of the matrix. For example, the command C[ 2, 3 ] produces the message Error: subscript out of bounds. Note that we can obtain the dimensions of a matrix using the function dim, e.g. dim(A) returns c(3,2).

Now that we know how to extract data from a matrix, we can edit its content by using subsetting to identify a particular element and assigning a new value to that position. For example:

C

C[ 2, 2 ] <- 4

C

Note that the number in the second row and second column had changed from 2 to 4.