### Creating Matrices

A matrix is a table of data. There are a few ways to make a matrix. One way is to “bind” together a bunch of vectors.

v1 <- c(3,4,4,5,2)
v2 <- c(5,6,7,7,9)
v3 <- c(1,1,3,8,0)
v4 <- c(1,4,5,5,3)

#the function rbind() will bind the vectors to form the rows of a matrix
M.r <- rbind(v1, v2, v3, v4)

#the function cbind() will bind the vectors to form the columns of a matrix
M.c <- cbind(v1, v2, v3, v4)
M.r
##    [,1] [,2] [,3] [,4] [,5]
## v1    3    4    4    5    2
## v2    5    6    7    7    9
## v3    1    1    3    8    0
## v4    1    4    5    5    3
M.c
##      v1 v2 v3 v4
## [1,]  3  5  1  1
## [2,]  4  6  1  4
## [3,]  4  7  3  5
## [4,]  5  7  8  5
## [5,]  2  9  0  3

The size or dimension of a matrix is a pair of integers giving the number of rows and the number of columns in that order. The function dim() can be called to check the size of a matrix:

dim(M.r)
##  4 5

This informs us that M.r has $$4$$ rows and $$5$$ columns.

dim(M.c)
##  5 4

M.c has $$5$$ rows and $$4$$ columns.

The related functions nrow() and ncol() can be used to tell us the number of rows and the number of columns respectively for a given matrix:

nrow(M.r)
##  4
ncol(M.r)
##  5

Anorther way to create a matrix is to feed a single vector into the function matrix(). Here are some examples of its use.

v <- c(2,5,-1,3,2,5,-9,0,8,2,3,4,5,2,7,8,6,4,4,3)
length(v)
##  20
A.v <- matrix(v, nrow = 4)
A.v
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    2    8    5    6
## [2,]    5    5    2    2    4
## [3,]   -1   -9    3    7    4
## [4,]    3    0    4    8    3

Note that by default matrix() populates the columns with the vector elements. This can easily be changed:

B.v <- matrix(v, nrow = 4, byrow = TRUE)
B.v
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    5   -1    3    2
## [2,]    5   -9    0    8    2
## [3,]    3    4    5    2    7
## [4,]    8    6    4    4    3

We can instead specify the number of columns required:

C.v <- matrix(v, ncol = 2, byrow = TRUE)
C.v
##       [,1] [,2]
##  [1,]    2    5
##  [2,]   -1    3
##  [3,]    2    5
##  [4,]   -9    0
##  [5,]    8    2
##  [6,]    3    4
##  [7,]    5    2
##  [8,]    7    8
##  [9,]    6    4
## [10,]    4    3

In each of the cases above, the number of rows or columns specified for the matrix neatly divides the length of the vector used to populate it. If this is not the case, R will generate a warning and will create a matrix with the required number of rows / columns but will cycle through the vector to fill in the “extra spaces”. For example:

D.v <- matrix(v, ncol = 6, byrow = TRUE)
## Warning in matrix(v, ncol = 6, byrow = TRUE): data length  is not a
## sub-multiple or multiple of the number of columns 
D.v
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    2    5   -1    3    2    5
## [2,]   -9    0    8    2    3    4
## [3,]    5    2    7    8    6    4
## [4,]    4    3    2    5   -1    3

Notice in this example that the matrix has been populated from v row by row until position $$(4, 2)$$ of the matrix. At this point, we have run out of elements in v, so R continues populating the matrix from the beginning of the vector. Thus, $$2, 5, -1, 3$$ fill in the extra spaces left over.

### Matrix Arithmetic

#### 1) Scalar Arithmetic

You can add, subtract, multiply and divide matrices by numbers component-wise:

M.r + 8
##    [,1] [,2] [,3] [,4] [,5]
## v1   11   12   12   13   10
## v2   13   14   15   15   17
## v3    9    9   11   16    8
## v4    9   12   13   13   11
M.c - 10
##      v1 v2  v3 v4
## [1,] -7 -5  -9 -9
## [2,] -6 -4  -9 -6
## [3,] -6 -3  -7 -5
## [4,] -5 -3  -2 -5
## [5,] -8 -1 -10 -7
3*M.r
##    [,1] [,2] [,3] [,4] [,5]
## v1    9   12   12   15    6
## v2   15   18   21   21   27
## v3    3    3    9   24    0
## v4    3   12   15   15    9
M.c/2
##       v1  v2  v3  v4
## [1,] 1.5 2.5 0.5 0.5
## [2,] 2.0 3.0 0.5 2.0
## [3,] 2.0 3.5 1.5 2.5
## [4,] 2.5 3.5 4.0 2.5
## [5,] 1.0 4.5 0.0 1.5

#### 2) Componentwise Matrix Addition and Subtraction

Provided two matrices are of the same size, they can be added or subtracted. First let’s make two matrices of equal size:

A <- M.r
B <- 3*M.r
A
##    [,1] [,2] [,3] [,4] [,5]
## v1    3    4    4    5    2
## v2    5    6    7    7    9
## v3    1    1    3    8    0
## v4    1    4    5    5    3
B
##    [,1] [,2] [,3] [,4] [,5]
## v1    9   12   12   15    6
## v2   15   18   21   21   27
## v3    3    3    9   24    0
## v4    3   12   15   15    9

Addition and subtraction is carried out component-wise.

A + B
##    [,1] [,2] [,3] [,4] [,5]
## v1   12   16   16   20    8
## v2   20   24   28   28   36
## v3    4    4   12   32    0
## v4    4   16   20   20   12
A - B
##    [,1] [,2] [,3] [,4] [,5]
## v1   -6   -8   -8  -10   -4
## v2  -10  -12  -14  -14  -18
## v3   -2   -2   -6  -16    0
## v4   -2   -8  -10  -10   -6

#### 3) Componentwise Matrix Multiplication and Division

Provided two matrices are of the same size, the can be multiplied or divided in componentwise fashion:

w1 <- c(2,4)
w2 <- c(3,8)

C <- rbind(w1, w2)
D <- cbind(w1, w2)

C
##    [,1] [,2]
## w1    2    4
## w2    3    8
D
##      w1 w2
## [1,]  2  3
## [2,]  4  8

Now let’s componentwise multiply and divide these matrices:

C*D
##    [,1] [,2]
## w1    4   12
## w2   12   64
C/D
##    [,1]     [,2]
## w1 1.00 1.333333
## w2 0.75 1.000000

#### 4) Mathematical Matrix Multiplication, Identity and Inverses

This section requires some knowledge of matrices, determinants and inverses. Feel free to skip over it if you have not met these ideas before.

Mathematical matrix multiplication works differently from componentwise matrix multiplication. Briefly, this is how it works. In order to form the product of two matrices, $$C = A \times B$$, we must have ncol(A) = nrow(B). When this holds, the value in row $$i$$ and column $$j$$ of $$C$$ is given by:

$C_{i,j} = \sum_{k = 1}^{r} A_{i,k}B_{k,j}$ where $$r$$ = ncol(A) = ncol(B). If you have met this idea before, this should be familiar but don’t worry if not. A little practice and you’ll see it’s not as difficult as the description above might make it seem!

In order to get R to mathematically-multiply two matrices as opposed to componentwise multiply two matrices, we need to use the command %*%.

M.r %*% M.c
##     v1  v2 v3  v4
## v1  70 120 59  70
## v2 120 240 88 126
## v3  59  88 75  60
## v4  70 126 60  76
M.c %*% M.r
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   36   47   55   63   54
## [2,]   47   69   81   90   74
## [3,]   55   81   99  118   86
## [4,]   63   90  118  163   88
## [5,]   54   74   86   88   94

The determinant of a matrix is easily found using the command det():

det(C)
##  4

When the determinant is non-zero, the inverse of a matrix can be found using solve():

X <- solve(C)

Finally we check that the product of a matrix with its inverse gives the identity matrix, as it should:

X %*% C
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
C %*% X
##    w1 w2
## w1  1  0
## w2  0  1