NOTE 5: MATRICES IN R
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 1
• Matrix is a two dimensional data structure.
2 5
21 1 0 18 1×4
12 8 2×2
• It is a vector with additional dimension attributes; number of rows and columns.
• Matrices contain elements of the same atomic types.
Logical: 𝑇𝑅𝑈𝐸 𝐹𝐴𝐿𝑆𝐸
Integer: 1 1
𝐹𝐴𝐿𝑆𝐸 𝑇𝑅𝑈𝐸 2×2 0
1 2×2
𝑀𝑜𝑛𝑑𝑎𝑦
Double: 1.25 0.5 Character:
9.87 5.9 2×2 𝑇𝑢𝑒𝑠𝑑𝑎𝑦 2×1
• Matrix containing only characters or only logical values are not of much use.
• Matrices containing numeric elements can be used in mathematical calculations.
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 2
Creating a Matrix
• The basic syntax for creating a matrix in R is:
matrix(vector, nrow=r, ncol=c, byrow=TRUE)
• vector: the input vector which becomes the data elements of the matrix
• nrow: the number of rows to be created
Dimensions of
• ncol: the number of columns to be created the matrix
• byrow: a logical clue. If TRUE (or T) then the input vector elements are
arranged by row
• Providing values for both dimensions is not necessary. If one of the dimensions
is provided, the other is inferred from length of the data.
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 3
• Ex:
>A <- matrix(1:9,nrow=3,ncol=3,byrow=T)
>A <- matrix(1:9,nrow=3,byrow=T)
We can define the column and row names
>rownames(A)=c("row1", "row2", "row3")
>colnames(A)=c("col1", "col2", "col3")
>A
col1 col2 col3
row1 1 2 3
row2 4 5 6
row3 7 8 9
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 4
• To find the dimensions of matrix A;
>dim(A)
[1] 3 3
• To find the transpose of matrix A;
>t(A)
row1 row2 row3
col1 1 4 7
col2 2 5 8
col3 3 6 9
• To find the diagonal elements of matrix A;
>diag(A)
[1] 1 5 9
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 5
Exercise 1
1. Generate a sequence starting from 1 to 30 with an increment of 2 and name
it “Seq1”.
2. Create a matrix ‘M1’ using the values of ‘Seq1’ with 3 rows by entering the
values column wise.
3. Define the rows as ‘A’, ‘B’ and ‘C’.
4. Define the columns as ‘P’, ‘Q’ , ‘R’, ‘S’ and ‘T’.
5. Find the dimensions of “M1”.
6. Find the transpose of “M1”.
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 6
Answer
1. >Seq1 <-seq(from=1, to=30, by=2)
2. >M1 <-matrix(Seq1,nrow=3,byrow=FALSE)
3. >rownames(M1) <-c( "A", "B", "C")
4. >colnames(M1) <-c( "P", "Q", "R", "S", "T")
>M1
P Q R S T
A 1 7 13 19 25
B 3 9 15 21 27
C 5 11 17 23 29
4. >dim(M1)
5. >t(M1)
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 7
Square Matrix
• A square matrix is a 𝑛 × 𝑛 matrix with the same number of rows and columns.
Properties:
• The number of rows is equal to number of columns.
• The determinant of a matrix can only be calculated for a square matrix.
• Trace of a matrix is equal to the sum of diagonal elements of the square
matrix.
• Inverse of matrix is calculated only for a square matrix.
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 8
Determinant of a Matrix
• The determinant helps us to find the inverse of a matrix.
• It tells us things about the matrix that are useful in systems of linear
equations, calculus and more.
• The symbol for determinant is two vertical lines either side.
• For a 2 × 2 matrix the determinant is:
𝑎 𝑏
𝐴= 𝑑𝑒𝑡𝐴 = 𝐴 = 𝑎𝑑 − 𝑏𝑐
𝑐 𝑑 2×2
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 9
Inverse of a Matrix
• The inverse of 𝐴 is 𝐴−1 only when:
𝐴 × 𝐴−1 = 𝐴−1 × 𝐴 = 𝐼
• For a 2 × 2 matrix the inverse is:
−1 1
𝑎 𝑏 𝑎 𝑏 𝑑 −𝑏
𝐴= 𝐿𝑒𝑡 𝐵 = 𝐴−1 = =
𝑐 𝑑 2×2 𝑐 𝑑 𝑎𝑑 − 𝑏𝑐 −𝑐 𝑎
• To find inverse of a matrix,
I. The matrix must be a square matrix (a matrix with the same number of
rows and columns).
II. The determinant of the matrix must not be equal to zero.
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 10
3 6
• Ex: Consider the matrix 𝐵 =
9 12 2×2
o Matrix 𝐵 is a square matrix.
o To find the determinant of matrix 𝐵 ;
>det(B)
[1] -18
o Determinant is not equal to zero, therefore, the inverse of matrix 𝐵 exists.
o Tofind the inverse of matrix 𝐵;
>solve(B)
[,1] [,2]
[1,] -0.6666667 0.3333333
[2,] 0.5000000 -0.1666667
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 11
Diagonal Matrix
• A diagonal matrix is a matrix in which the entries outside the main diagonal are all zero;
the term usually refers to square matrices.
• Ex: To create a 3 × 3 diagonal matrix with values 2, 3, 5 in its diagonal
>M1 <- diag(c(2,3,5))
>M1
[,1] [,2] [,3]
[1,] 2 0 0
[2,] 0 3 0
[3,] 0 0 5
• Ex: To create a 5 × 5 diagonal matrix with the number 3 in its diagonal
>M2 <- diag(3,nrow=5)
>M2
[,1] [,2] [,3] [,4] [,5]
[1,] 3 0 0 0 0
[2,] 0 3 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 3 0
[5,] 0 0 0 0 3
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 12
Exercise 2
1. Create the matrix 𝐴, where its diagonal values are 1, 2, 3, and 4.
2. Set the row names of 𝐴 as “a”, “b”, “c” and “d” and column names as “p”, “q”, “r”
and “s”.
3. Find the dimensions of matrix 𝐴.
4. Find the determinant of matrix 𝐴.
5. Find matrix 𝐵, where 𝐵 is the transpose of matrix 𝐴.
6. Find the inverse of matrix 𝐴 and name it as matrix 𝐶.
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 13
Accessing Elements in a Matrix
2 3 6
Consider the following matrix; 𝑚2 = 5 8 7
4 9 1
• To find the 2nd element of 𝑚2 >m2[2]
• To find the 1st row of 𝑚2 >m2[1,]
• To find the 3rd column of 𝑚2 >m2[,3]
• To find the element in the 3rd row 2nd column of 𝑚2 >m2[3,2]
• To find the 2nd and 3rd rows of 𝑚2 >m2[2:3,]
• To find the 1st and 3rd columns of 𝑚2 >m2[,c(1,3)]
• To find the elements of 𝑚2, except in column 1 and 3 >m2[,-c(1,3)]
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 14
Modifying Elements of a Matrix
• To replace the value in the 2nd row 3rd column of 𝑚2 >m2[2,3]<-11
with 11
• To change the values in the 3rd row of 𝑚2 to 4,7,15 >m2[3,]<-c(4,7,15)
• To change the values in first 2 rows, last 2 columns of >m2[1:2,2:3]<-c(4,3,2,1)
𝑚2 to 4,3,2,1
• To add another row to 𝑚2 as 5,6,7 >rbind(m2,c(5:7))
• To add another column to 𝑚2 as 6, 10, 21 >cbind(m2,c(6,10,21))
• To remove the last row of 𝑚2 >m2<-m2[1:2,]
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 15
• You can create empty matrices and later add elements to it.
>M3<-matrix(nrow=3,ncol=2)
>M3
[,1] [,2]
[1,] NA NA
[2,] NA NA
[3,] NA NA [,1] [,2]
[1,] 5 14
1. Add the values in a sequence from 5 to 20 with space 3 [2,] 8 17
[3,] 11 20
>M3[1:3,1:2]<-seq(5,20,3)
[,1] [,2]
[1,] 1 NA
2. Fill the 1st column of the empty matrix with 1,2,3 [2,] 2 NA
>M3[,1]<-c(1:3) [3,] 3 NA
[,1] [,2]
3. Fill the 1st and 3rd row of the empty matrix with 2,3,5,7 [1,] 2 5
>M3[c(1,3),]<-c(2,3,5,7) [2,] NA NA
[3,] 3 7
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 16
Arithmetic Operations in Matrices
• To add or subtract two matrices, dimensions of both matrices should be equal.
1 2 5 6 1 2 5 6 7
+ +
3 4 2×2 7 8 2×2 3 4 2×2 8 9 10 2×3
• For matrix multiplication, the number of columns in the first matrix must be
equal to the number of rows in the second matrix.
1 2 5 6
7 8 1 2
3 4 × × 7 8
9 10 2×2 3 4 2×2
5 6 3×2 9 10 3×2
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 17
• Create a 3 × 5 matrix 𝑚3 by including the integer values from 11 to 25 row-wise.
>m3 <- matrix(c(11:25), nrow=3, byrow=T)
>m3
[,1] [,2] [,3] [,4] [,5]
[1,] 11 12 13 14 15
[2,] 16 17 18 19 20
[3,] 21 22 23 24 25
• Create a 3 × 5 matrix 𝑚4 by including the integer values from 21 to 35 column-wise.
>m4 <- matrix(c(21:35), nrow=3, byrow=F)
>m4
[,1] [,2] [,3] [,4] [,5]
[1,] 21 24 27 30 33
[2,] 22 25 28 31 34
[3,] 23 26 29 32 35
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 18
• Addition >m3+m4 [,1] [,2] [,3] [,4] [,5]
[1,] 32 36 40 44 48
add 𝑚3 to 𝑚4 [2,] 38 42 46 50 54
[3,] 44 48 52 56 60
• Subtraction >m3-m4 [,1] [,2] [,3] [,4] [,5]
[1,] -10 -12 -14 -16 -18
subtract 𝑚4 from 𝑚3 [2,] -6 -8 -10 -12 -14
[3,] -2 -4 -6 -8 -10
• Multiplication >m3%*%t(m4) [,1] [,2] [,3]
[1,] 1785 1850 1915
multiply 𝑚3 with the transpose [2,] 2460 2550 2640
of 𝑚4 [3,] 3135 3250 3365
• Element-wise multiplication >m3*m4 [,1] [,2] [,3] [,4] [,5]
[1,] 231 288 351 420 495
can be done only if the dimensions [2,] 352 425 504 589 680
of both the matrices are equal [3,] 483 572 667 768 875
STA1031 DEPARTMENT OF STATISTICS AND COMPUTER SCIENCE 19