Matrix Operations
Matrix Operations
In a previous section, we motivated the use of matrix algebra with this system of equations:
We described how this system can be rewritten and solved using matrix algebra:
Having described matrix notation, we will explain the operation we perform with them. For example, above we have matrix multiplication and we also have a symbol representing the inverse of a matrix. The importance of these operations and others will become clear once we present specific examples related to data analysis.
Multiplying by a scalar
We start with one of the simplest operations: scalar multiplication. If is scalar and is a matrix, then:
R automatically follows this rule when we multiply a number by a matrix using *
:
X <- matrix(1:12,4,3)
print(X)
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
a <- 2
print(a*X)
## [,1] [,2] [,3]
## [1,] 2 10 18
## [2,] 4 12 20
## [3,] 6 14 22
## [4,] 8 16 24
The transpose
The transpose is an operation that simply changes columns to rows. We use a to denote a transpose. The technical definition is as follows: if X is as we defined it above, here is the transpose which will be :
In R we simply use t
:
X <- matrix(1:12,4,3)
X
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
t(X)
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
Matrix multiplication
We start by describing the matrix multiplication shown in the original system of equations example:
What we are doing is multiplying the rows of the first matrix by the columns of the second. Since the second matrix only has one column, we perform this multiplication by doing the following:
Here is a simple example. We can check to see if abc=c(3,2,1)
is a solution:
X <- matrix(c(1,3,2,1,-2,1,1,1,-1),3,3)
abc <- c(3,2,1) #use as an example
rbind( sum(X[1,]*abc), sum(X[2,]*abc), sum(X[3,]%*%abc))
## [,1]
## [1,] 6
## [2,] 6
## [3,] 7
We can use the %*%
to perform the matrix multiplication and make this much more compact:
X%*%abc
## [,1]
## [1,] 6
## [2,] 6
## [3,] 7
We can see that c(3,2,1)
is not a solution as the answer here is not the required c(6,2,1)
.
To get the solution, we will need to invert the matrix on the left, a concept we learn about below.
Here is the general definition of matrix multiplication of matrices and :
You can only take the product if the number of columns of the first matrix equals the number of rows of the second one . Also, the final matrix has the same row numbers as the first and the same column numbers as the second . After you study the example below, you may want to come back and re-read the sections above.
The identity matrix
The identity matrix is analogous to the number 1: if you multiply the identity matrix by another matrix, you get the same matrix. For this to happen, we need it to be like this:
By this definition, the identity always has to have the same number of rows as columns or be what we call a square matrix.
If you follow the matrix multiplication rule above, you notice this works out:
In R you can form an identity matrix this way:
n <- 5 #pick dimensions
diag(n)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 0
## [2,] 0 1 0 0 0
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 0
## [5,] 0 0 0 0 1
The inverse
The inverse of matrix , denoted with , has the property that, when multiplied, gives you the identity . Of course, not all matrices have inverses. For example, a matrix with 1s in all its entries does not have an inverse.
As we will see when we get to the section on applications to linear models, being able to compute the inverse of a matrix is quite useful. A very convenient aspect of R is that it includes a predefined function solve
to do this. Here is how we would use it to solve the linear of equations:
X <- matrix(c(1,3,2,1,-2,1,1,1,-1),3,3)
y <- matrix(c(6,2,1),3,1)
solve(X)%*%y #equivalent to solve(X,y)
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
Please note that solve
is a function that should be used with caution as it is not generally numerically stable. We explain this in much more detail in the QR factorization section.