7.3 Operations on Matrices

Matrices can be involved in arithmetical and logical operations.

7.3.1 Arithmetical Operations

The usual arithmetic operations apply to matrices, operating element-wise. For example, suppose that we have:

To get the sum of the above two matrices, R adds their corresponding elements and forms a new matrix out of their sums, thus:

##      [,1] [,2]
## [1,]    3    3
## [2,]    3    3

R applies recycling as needed. For example, suppose we have:

##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

In order to multiply each element of mat by 2, we need not create a 2-by-2 matrix of 2’s. We can simply multiply by 2, and R will take care of recycling the 2:

##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8

Or we could subtract 3 from each element of mat:

##      [,1] [,2]
## [1,]   -2    0
## [2,]   -1    1

7.3.2 Matrix Multiplication

This section is optional reading, but it may interest you if you know about matrix multiplication in linear algebra.

In order to accomplish matrix multiplication, we have to keep in mind that the regular multiplication operator * works element-wise on matrices, as we have already seen. For matrix multiplication R provides the special operator %*%. For example, consider the following matrices:

##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
##      [,1]
## [1,]    2
## [2,]    1
## [3,]   -1

Observe that the number of columns of a is equal to the number of rows of b. Hence it is possible to form the matrix product a %*% b:

##      [,1]
## [1,]    0
## [2,]    2

As expected, the result is a matrix having as many rows as the rows of aand as many columns as the columns of b.

It is also interesting to recall how matrix multiplication works when the second matrix has only one column. The product is obtained by multiplying each column of a by the element on the corresponding row of b, and adding the resulting matrices:

##      [,1]
## [1,]    0
## [2,]    2

7.3.3 Logical Operations

Boolean operations apply to matrices element-wise, just as they do to ordinary vectors. The result is a matrix of logical values. For examples, consider the original matrix numbersMat:

Suppose we wish to determine which elements of numbersMat are odd. Then we simply ask whether the remainder of an element after division by 2 is equal to 1:

##       [,1]  [,2]  [,3]  [,4]
## [1,]  TRUE  TRUE  TRUE  TRUE
## [2,] FALSE FALSE FALSE FALSE
## [3,]  TRUE  TRUE  TRUE  TRUE
## [4,] FALSE FALSE FALSE FALSE
## [5,]  TRUE  TRUE  TRUE  TRUE
## [6,] FALSE FALSE FALSE FALSE

We can select elements from a matrix using a Boolean operator, too:

##  [1]  1  3  5  7  9 11 13 15 17 19 21 23

Note that the result is an ordinary, one-dimensional vector.

7.3.4 Practice Exercises

We’ll work with the following three matrices:

##      [,1] [,2]
## [1,]    7    9
## [2,]    4   10
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
##      [,1] [,2] [,3] [,4]
## [1,] "a"  "b"  "c"  "d" 
## [2,] "e"  "f"  "g"  "h" 
## [3,] "i"  "j"  "k"  "l" 
## [4,] "m"  "n"  "o"  "p" 
## [5,] "q"  "r"  "s"  "t" 
## [6,] "u"  "v"  "w"  "x"
  1. Find a one-line command using a that results in:

    ##      [,1] [,2]
    ## [1,]   10   12
    ## [2,]    7   13
  2. Find a one-line command using a that results in:

    ##      [,1] [,2]
    ## [1,]   14   18
    ## [2,]    8   20
  3. Find a one-line command using a that results in:

    ##      [,1] [,2]
    ## [1,]   49   81
    ## [2,]   16  100
  4. Find a one-line command using a and b that results in:

    ##      [,1] [,2]
    ## [1,]    6    6
    ## [2,]    2    6
  5. Describe in words what the following command does:

  6. Write a one-line command using a that tells you which elements of a are one more than a multiple of 3.

  7. Using c, write a one-line boolean expression that produces the following:

    ##       [,1]  [,2]  [,3]  [,4]
    ## [1,] FALSE FALSE FALSE FALSE
    ## [2,] FALSE FALSE FALSE  TRUE
    ## [3,]  TRUE  TRUE  TRUE  TRUE
    ## [4,]  TRUE  TRUE  TRUE  TRUE
    ## [5,]  TRUE  TRUE  TRUE  TRUE
    ## [6,]  TRUE  TRUE  TRUE  TRUE

7.3.5 Solutions to Practice Exercises

  1. Here’s one way:

  2. Here’s one way:

  3. Here’s one way:

  4. Here’s one way:

```r
a - b
```
  1. It produces a logical matrix of the same dimensions as a. The new matrix will have TRUE in a cell when the corresponding cell of a is greater than 5. Otherwise, the cell will have FALSE in it.

  2. Here’s one way:

  3. Here’s one way: