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:
<- matrix(rep(1, 4), nrow = 2)
mat1 <- matrix(rep(2, 4), nrow = 2) mat2
To get the sum of the above two matrices, R adds their corresponding elements and forms a new matrix out of their sums, thus:
+ mat2 mat1
## [,1] [,2]
## [1,] 3 3
## [2,] 3 3
R applies recycling as needed. For example, suppose we have:
<- matrix(1:4, nrow = 2)
mat mat
## [,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:
2 * mat
## [,1] [,2]
## [1,] 2 6
## [2,] 4 8
Or we could subtract 3 from each element of mat
:
- 3 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:
<- matrix(1:6, ncol = 3)
a a
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
<- matrix(c(2, 1, -1), nrow = 3)
b b
## [,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
:
%*% b a
## [,1]
## [1,] 0
## [2,] 2
As expected, the result is a matrix having as many rows as the rows of a
and 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]*a[ ,1, drop = FALSE] + b[2,1, drop = FALSE]*a[ ,2] + b[3,1]*a[ ,3, drop = FALSE] b[
## [,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
:
<- matrix(1:24, nrow = 6) 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:
%% 2 == 1 numbersMat
## [,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:
%% 2 == 1] numbersMat[numbersMat
## [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:
<- matrix(c(7, 4, 9, 10), nrow = 2)
a a
## [,1] [,2]
## [1,] 7 9
## [2,] 4 10
<- matrix(1:4, nrow = 2)
b b
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
<- matrix(letters[1:24], nrow = 6, byrow = TRUE)
c c
## [,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"
Find a one-line command using
a
that results in:## [,1] [,2] ## [1,] 10 12 ## [2,] 7 13
Find a one-line command using
a
that results in:## [,1] [,2] ## [1,] 14 18 ## [2,] 8 20
Find a one-line command using
a
that results in:## [,1] [,2] ## [1,] 49 81 ## [2,] 16 100
Find a one-line command using
a
andb
that results in:## [,1] [,2] ## [1,] 6 6 ## [2,] 2 6
Describe in words what the following command does:
> 5 a
Write a one-line command using
a
that tells you which elements ofa
are one more than a multiple of 3.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
Here’s one way:
+ 3 a
Here’s one way:
2 * a
Here’s one way:
^2 a
Here’s one way:
```r
a - b
```
It produces a logical matrix of the same dimensions as
a
. The new matrix will haveTRUE
in a cell when the corresponding cell ofa
is greater than 5. Otherwise, the cell will haveFALSE
in it.Here’s one way:
%% 3 == 1 a
Here’s one way:
>= "h" c