This set of exercises will help you to learn and test your skill in matrix operations, starting with basic ones like scalar multiplication all the way through eigenvalue and eigenvectors. Before proceeding, it might be helpful to look over the help pages for the diag, t, eigen, and crossprod functions. If you want further documentation also consider chapter 5.7 from “An Introduction to R”.

4.4.1 Consider A=matrix(c(2,0,1,3), ncol=2) and B=matrix(c(5,2,4,-1), ncol=2).
a) Find A + B
b) Find A – B

A = matrix(c(2,0,1,3), ncol=2)
B = matrix(c(5,2,4,-1), ncol=2)

A+B
##      [,1] [,2]
## [1,]    7    5
## [2,]    2    2
A-B
##      [,1] [,2]
## [1,]   -3   -3
## [2,]   -2    4

4.4.2 Scalar multiplication. Find the solution for aA where a=3 and A is the same as in the previous question.

a <- 3

a*A
##      [,1] [,2]
## [1,]    6    3
## [2,]    0    9

4.4.3 Using the the diag function build a diagonal matrix of size 4 with the following values in the diagonal 4,1,2,3.

diag(x = c(4,1,2,3), nrow = 4, ncol = 4)
##      [,1] [,2] [,3] [,4]
## [1,]    4    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    2    0
## [4,]    0    0    0    3
#Alternative solution: diag(4)*c(4,1,2,3)

4.4.4 Find the solution for Ab, where A is the same as in the previous question and b=c(7,4).

b <- c(7,4)

b%*%A
##      [,1] [,2]
## [1,]   14   19

4.4.5 Find the solution for AB, where B is the same as in question 1.

A%*%B
##      [,1] [,2]
## [1,]   12    7
## [2,]    6   -3

4.4.6 Find the transpose matrix of A.

A
##      [,1] [,2]
## [1,]    2    1
## [2,]    0    3
t(A)
##      [,1] [,2]
## [1,]    2    0
## [2,]    1    3

4.4.7 Find the inverse matrix of A.

solve(A)
##      [,1]       [,2]
## [1,]  0.5 -0.1666667
## [2,]  0.0  0.3333333

4.4.8 Find the value of x on Ax=b.

solve(A,b) #Very useful. Excel has the same function too.
## [1] 2.833333 1.333333

4.4.9 Using the function eigen find the eigenvalue for A.

eigen(A)
## $values
## [1] 3 2
## 
## $vectors
##           [,1] [,2]
## [1,] 0.7071068    1
## [2,] 0.7071068    0
eigen(A)$values #will give just eigen values for A.
## [1] 3 2

4.4.10 Find the eigenvalues and eigenvectors of A’A . Hint: Use crossprod to compute A’A .

#It's a useful hint.

eigen(crossprod(A))$values
## [1] 10.605551  3.394449
eigen(crossprod(A))$vectors
##           [,1]       [,2]
## [1,] 0.2897841 -0.9570920
## [2,] 0.9570920  0.2897841