0% found this document useful (0 votes)
15 views5 pages

BSTA 3152 Lecture One Matrices

Uploaded by

jumajohn0013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

BSTA 3152 Lecture One Matrices

Uploaded by

jumajohn0013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BSTA 3152 Statistical Programming : Lecture 1

BSc BSIT, BASC,BAST

Semester: September to December 2023

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)

We can assign column and row names to a matrix


M<-matrix(1:20,ncol=4)
colnames(M)<-c("A","B","C","D")
rownames(M)<-c("E","F","G","H","I")

Extracting Row/Column from Matrices


We can extract more than one row/column at a time
# creating a matrix
MatA <- matrix(c(2,8,9,7), nrow = 2, ncol = 2, byrow = TRUE)
# extracting the element in first row and second column
MatA[1,2]
#extracting entire row
MatA[1,]
#extracting entire column
MatA[,2]

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

B <- matrix(c(5,6,7,8),2,2, byrow = TRUE)


sum <- A + B

To check the dimension of a matrix we use the function dim()


# checking the dimension of a matrix
D <- matrix(c(4,5,7,8),2,2, byrow = TRUE)
dim(D)

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)

Sums of rows in Matrix

# determinant of a matrix
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
rowSums(A)

By Kipngetich Gideon [email protected] 2


Operations in Matrices MATRICES IN R

Sums of columns in Matrix

# determinant of a matrix
A <- matrix(c(1,2,3,4), 2,2, byrow = TRUE)
colSums(A)

Replacing elements of a Matrix

Matb <- matrix(1,3,3)


# replacing one element
Matb[1,1] = 2
# replacing entire row
Matb[1,] = 2

Diagonal matrices and identity matrices


A diagonal matrix is one that is square, meaning number of rows equals number of columns, and it has 0s on
the off-diagonal and non-zeros on the diagonal.
You can create a diagonal matrix with the diag() function
#put 1 on diagonal of 3x3 matrix
diag(1,3)
#put 1 to 4 on diagonal of 4x4 matrix
diag(1:4)

The diag() function can also be used to replace elements on the diagonal of a matrix:
A=matrix(3, 3, 3)
diag(A)=1

The diag function is also used to get the diagonal of a matrix.


A=matrix(1:9, 3, 3)
diag(A)

Eigen values and Eigen vectors of Matrix


Eigenvalues and eigenvectors prominently appear in many statistical and other computational fields that
require transformations of linear systems or are interested in the evolution of systems from an initial point.
An eigenvalue is denoted by lambda λ and is defined as a scalar that when multiplied by a non-zero vector x
satisfies the following

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

By Kipngetich Gideon [email protected] 3


Operations in Matrices MATRICES IN R

The lambda identity matrix λI is thus:


 
1 1 −2
λI = −1 2 1 
0 1 −1
Therefore the characteristic equation is given as:
 
1−λ 1 −2
|A − λI| = det  −1 2−λ 1 
0 1 −1 − λ
For an n × n matrix, there will be n roots and thus n eigen values.

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

Thus the eigenvalues of the matrix A are 2, 1, and -1.


We can confirm this result in R using the eigen() function.
# Reading the matrix
A <- matrix(c(1,1,-2,-1,2,1,0,1,-1), 3,3, byrow = T)
# computing the eigen values and vectors
e <- eigen(A)
e$values
e$vectors

Kronecker Product of two matrices


Given a m × n matrix A and p × q matrix B, their kronecker product C also called their matrix direct product
is an mn × nq matrix.
The matrix direct(kronecker) product of the 2 × 2 matrix A and the 2 × 2 matrix B is given by the 4 × 4
 
1 2
A=
3 4
 
0 5
B=
6 7
 
0 5 0 10
 6 7 12 14
C=  0 15 0 20

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

By Kipngetich Gideon [email protected] 4


Operations in Matrices MATRICES IN R

 
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

## [,1] [,2] [,3]


## [1,] 1 2 3
## [2,] 4 2 6
## [3,] -3 -1 -3
Show that B × B × B is a scalar multiple of the identity matrix and find the scalar

By Kipngetich Gideon [email protected] 5

You might also like