BSTA 3152 Lecture One Matrices
BSTA 3152 Lecture One Matrices
Lecture Topics
• Matrx computation: identity, determinant, inverse and matrix factorisation, Matrix operations
Matrices in R
Matrices are two-dimensional data structures in R and are arranged in a rectangular layout.
Matrices can contain only one data type. A matrix can be thought of as a vector in two dimension.
To create a matrix in R we use the function matrix()
# creating a simple matrix
A <- matrix(1:6,nrow = 3, ncol = 2)
B <- matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE)
Operations in Matrices
Addition of two matrices
Matrices operations can be performed similar to how arithmetic operations are performed on numbers.
However we should be careful with the dimension of the numbers that we are working on
# matrices of equal dimensions
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
1
Operations in Matrices MATRICES IN R
Matrix Multiplication
Multiplying Matrices of same dimension The following operation using * is element wise
# multiplying matrices of equal dimensions element wise
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
B <- matrix(c(5,6,7,8),2,2, byrow = TRUE)
prod <- A * B
As a basic rule for matrix multiplication number of rows of Matrix 1 should be equal to number of columns
in Matrix 2 when multiplying Matrix 1 with Matrix 2
# multiplying matrices of equal dimensions
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
B <- matrix(c(5,6,7,8),2,2, byrow = TRUE)
prod <- A %*% B
Transposing a Matrix
This is used to interchange rows and columns i.e rows become columns and columns become rows in new
transposed matrix.
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
t(A)
Inverse of a matrix
Inverting a square matrix in R:
# inverse of a matrix
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
solve(A)
Determinant of a matrix
# determinant of a matrix
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
det(A)
# determinant of a matrix
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
rowSums(A)
# determinant of a matrix
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
colSums(A)
The diag() function can also be used to replace elements on the diagonal of a matrix:
A=matrix(3, 3, 3)
diag(A)=1
Ax = λx
The above equation can be written differently to express the desire to find λ and x
(A − λI)x = 0
To find the solutions that don’t end up as x =0, the matrix (A − λI) is set to |A − λI| = 0 which is called
the characteristic equation
For example, consider the following Matrix A
1 1 −2
−1 2 1
0 1 −1
Computation of eigenvalues
The method of finding the eigenvalues of an n × n matrix can be summarized into two steps:
• Find the determinant of the characteristic equation A − λI
• After the determinant is computed, find the roots (eigenvalues) of the resultant polynomial.
λ3 − 2λ2 − λ + 2
18 21 24 28
The matrix direct(kronecker) product of the 2 × 3 matrix A and the 3 × 2 matrix B is given by the 6 × 6
1 2
A = 3 4
1 0
0 5 2
B=
6 7 3
0 5 2 0 10 4
6 7 3 12 14 6
0 15 6 0 20 8
C=
18 21 9 24 28 12
0 5 2 0 0 0
6 7 3 0 0 0
A <- matrix(c(1,2,3,4,1,0),3,2,byrow = T)
B <- matrix(c(0,5,2,6,7,3),2,3,byrow = T)
kronecker(A,B)
Exercise
Construct the Matrix
B <- matrix(c(1,2,3,4,2,6,-3,-1,-3),3,3, byrow = TRUE)
B