Matrix Operations

Size

The length() function works with matrices, but the results may not be that interesting.

(m1 <- matrix(1:9, 3, 3))
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
length(m1)
[1] 9

Do: Explain what the value returned by length(m1) represents.

When applied to matrices, length() returns the number of cells in the matrix.

To get more useful information about the dimensions of the matrix, we use dim(), nrow(), or ncol().

# Number or rows and columns
dim(m1)
[1] 3 3
# Number of rows
nrow(m1)
[1] 3
# Number or columns
ncol(m1)
[1] 3

Arithmetic

As with vectors, default arithmetic with R matrices works element-wise. R performs the requested operation on each pair of corresponding entries in the two matrices.

(m2 <- matrix(sample(-5:5, 9, TRUE), 3, 3))
     [,1] [,2] [,3]
[1,]    4    5    4
[2,]   -1    3   -4
[3,]    1   -2   -4
m1 + m2
     [,1] [,2] [,3]
[1,]    5    9   11
[2,]    1    8    4
[3,]    4    4    5
m2 - m1
     [,1] [,2] [,3]
[1,]    3    1   -3
[2,]   -3   -2  -12
[3,]   -2   -8  -13
m1 / m2
      [,1]      [,2]  [,3]
[1,]  0.25  0.800000  1.75
[2,] -2.00  1.666667 -2.00
[3,]  3.00 -3.000000 -2.25
m1 * m2
     [,1] [,2] [,3]
[1,]    4   20   28
[2,]   -2   15  -32
[3,]    3  -12  -36
Practice

Use the three matrices defined below to answer this question.

a <- matrix(2, 2, 2)
a
     [,1] [,2]
[1,]    2    2
[2,]    2    2
b <- matrix(1:4, 2, 2)
b
     [,1] [,2]
[1,]    1    3
[2,]    2    4
c <- matrix(6, 2, 2)
c
     [,1] [,2]
[1,]    6    6
[2,]    6    6

Given the above definitions of a, b, and c, predict what the following code will print.

b + c / a

Use the interactive editor to check your prediction.

R will apply the usual order-of-operations when doing element-wise arithmetic with matrices.

We get the following matrix.

b + c / a
     [,1] [,2]
[1,]    4    6
[2,]    5    7

R first divides each element in c by the corresponding element in a.

(d <- c / a)
     [,1] [,2]
[1,]    3    3
[2,]    3    3

Then, the result of this division is added to b.

b + d
     [,1] [,2]
[1,]    4    6
[2,]    5    7
Matrix Algebra

If you’re familiar with matrix algebra and/or have some affinity for programming languages that overload their operators, the matrix arithmetic described above may seem very strange. Rather than overloading the standard operators, R defines special functions for matrix algebraic operations. For example:

  • %*%: Multiplication
  • t(): Transposition
  • solve(): Inversion
# Multiply m1 and m2 using true matrix multiplication
m1 %*% m2
     [,1] [,2] [,3]
[1,]    7    3  -40
[2,]   11    9  -44
[3,]   15   15  -48
# Transpose m1
t(m1)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
# Invert m2
solve(m2)
            [,1]        [,2]        [,3]
[1,] 0.161290323 -0.09677419  0.25806452
[2,] 0.064516129  0.16129032 -0.09677419
[3,] 0.008064516 -0.10483871 -0.13709677
Practice
  1. Create a \(5 \times 3\) numeric matrix called myMat wherein each column is equal to the vector 1:5.
  2. Multiply each entry in myMat by \(\pi\) (i.e., the mathematical constant, “pi”).

The built-in R object pi contains the value of \(\pi\).

(myMat <- matrix(1:5, 5, 3))
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3
[4,]    4    4    4
[5,]    5    5    5
myMat * pi
          [,1]      [,2]      [,3]
[1,]  3.141593  3.141593  3.141593
[2,]  6.283185  6.283185  6.283185
[3,]  9.424778  9.424778  9.424778
[4,] 12.566371 12.566371 12.566371
[5,] 15.707963 15.707963 15.707963
Knowledge Check
Back to top