Using matrix()
to create matrix:
matrix(c(1:8,99), nrow=3, byrow=T) # nrow - defining row number
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 99
matrix(c(1:8,99), ncol=3, byrow=T) # ncol - defining column number
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 99
mat<-matrix(c(1:8,99), nrow=3, ncol=3, byrow=F) # fills by column
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 99
byrow
is set to false by default.
To know details write help(matrix)
or ?matrix
and hit enter To check if the class of an object is matrix or not use
is.matrix()
-
is.matrix(mat)
[1] TRUE
rbind stands for row bind.
rbind(1:3,
5:7,
c(8,12,9))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 5 6 7
[3,] 8 12 9
cbind stands for column bind.
cmat <- cbind(1:3,
5:7,
c(8,12,9))
cmat
[,1] [,2] [,3]
[1,] 1 5 8
[2,] 2 6 12
[3,] 3 7 9
Use dim()
:
dim(mat)
[1] 3 3
Which shows that mat is a 3*3 matrix.
To know the number of columns use ncol()
:
ncol(mat)
[1] 3
To know the number of rows use nrow()
:
nrow(mat)
[1] 3
dmat<-matrix(data=10:18, nrow=3,
dimnames=list(c("A","B","C"),
c("D","E","F")))
dmat
D E F
A 10 13 16
B 11 14 17
C 12 15 18
To know the attributes of a matrix:
attributes(dmat)
$dim
[1] 3 3
$dimnames
$dimnames[[1]]
[1] "A" "B" "C"
$dimnames[[2]]
[1] "D" "E" "F"
Use det()
:
m<-matrix(c(3,2,4,3), ncol=2)
m
[,1] [,2]
[1,] 3 4
[2,] 2 3
det(m)
[1] 1
Let’s recall the matrix cmat:
cmat
[,1] [,2] [,3]
[1,] 1 5 8
[2,] 2 6 12
[3,] 3 7 9
Use matrixname[rowNumber,colNumber]
to select
elements:
cmat[1,2] #element of 1st row and 2nd column
[1] 5
cmat[1:2,3] #element of 1st and 2nd row, and 3rd column
[1] 8 12
cmat[,2] #select 2nd column
[1] 5 6 7
cmat[3,] #select 3rd row
[1] 3 7 9
cmat[c(1,3),] #select element from 1st and 3rd row
[,1] [,2] [,3]
[1,] 1 5 8
[2,] 3 7 9
cmat[,c(1,3)] #select element from 1st and 3rd column
[,1] [,2]
[1,] 1 8
[2,] 2 12
[3,] 3 9
Notice when we select a row or column, the matrix becomes a vector.
To select the data as matrix instead of vector, we can use
drop=FALSE
:
cmat[,1]
[1] 1 2 3
cmat[,1,drop=FALSE]
[,1]
[1,] 1
[2,] 2
[3,] 3
let’s recall the cmat matrix that we created:
cmat
[,1] [,2] [,3]
[1,] 1 5 8
[2,] 2 6 12
[3,] 3 7 9
If we wish to replace the 1st element then simply select the first element and replace it using <- :
cmat[1,1] <- 0
cmat
[,1] [,2] [,3]
[1,] 0 5 8
[2,] 2 6 12
[3,] 3 7 9
In a similar way we can replace multiple elements:
cmat[,1]<-c(9,9,9) #replace 1st column by 9,9,9
cmat
[,1] [,2] [,3]
[1,] 9 5 8
[2,] 9 6 12
[3,] 9 7 9
Let’s recall the mat matrix we created:
mat<-matrix(c(1:8,99), nrow=3, ncol=3,byrow=F)
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 99
To remove the 1st column:
mat[,-1]
[,1] [,2]
[1,] 4 7
[2,] 5 8
[3,] 6 99
mat isn’t replaced by the new matrix. We have to assign it to a new matrix or the old one:
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 99
mat<- mat[,-1]
mat
[,1] [,2]
[1,] 4 7
[2,] 5 8
[3,] 6 99
Similarly to remove the 2nd row,
mat[-2,]
[,1] [,2]
[1,] 4 7
[2,] 6 99
To find the diagonal elements of a matrix:
diag(cmat)
[1] 9 6 9
To create a diagonal matrix:
diag(c(12,1,99))
[,1] [,2] [,3]
[1,] 12 0 0
[2,] 0 1 0
[3,] 0 0 99
To create an identity matrix:
diag(3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
diag(5)
[,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
Let’s construct a matrix of order 3:
mat<-matrix(c(1:8,99), nrow=3,byrow=F)
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 99
To transpose it simply use t()
:
t(mat)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 99
A <- matrix(c(4, 2, 7, 8, 0, 2, 3, 9, 1), ncol = 3, byrow = TRUE)
A
[,1] [,2] [,3]
[1,] 4 2 7
[2,] 8 0 2
[3,] 3 9 1
Minor of the element in 1st row and 1st column -
det(A[-1,-1])
[1] -18
Minor of the element in 3rd row and 2nd column -
det(A[-3,-2])
[1] -48
Creating function -
minor <- function(A, i, j) {
det(A[-i,-j])
}
minor(A, 3, 2)
[1] -48
Co-factor of the element in 1st row and 2nd column -
(-1)^(1+2)*det(A[-1,-2])
[1] -2
In other way using the minor() function -
(-1)^(1+2)*minor(A, 1, 2)
[1] -2
Creating a new function -
cofactor <- function(A, i, j) (-1)^(i+j) * minor(A,i,j)
cofactor(A, 1, 2)
[1] -2
To calculate the adjoint matrix of the square matrix A -
A
[,1] [,2] [,3]
[1,] 4 2 7
[2,] 8 0 2
[3,] 3 9 1
Required portion of code -
t(outer(1:nrow(A), 1:nrow(A),
FUN = Vectorize(function(i,j) cofactor(A,i,j))))
[,1] [,2] [,3]
[1,] -18 61 4
[2,] -2 -17 48
[3,] 72 -30 -16
Creating function -
adjoint <- function(mat) {
t(outer(1:nrow(mat), 1:nrow(mat),
FUN = Vectorize(function(i,j) cofactor(mat,i,j))))
}
adjoint(A)
[,1] [,2] [,3]
[1,] -18 61 4
[2,] -2 -17 48
[3,] 72 -30 -16
Thus inverse of A -
adjoint(A)/det(A)
[,1] [,2] [,3]
[1,] -0.042056075 0.14252336 0.009345794
[2,] -0.004672897 -0.03971963 0.112149533
[3,] 0.168224299 -0.07009346 -0.037383178
Note: Vectorize()
vetorizes functions. Example -
sdf <- Vectorize(runif) # vectorizing runif
set.seed(0)
sdf(n=c(10,10,5,6))
[[1]]
[1] 0.8966972 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819 0.8983897
[8] 0.9446753 0.6607978 0.6291140
[[2]]
[1] 0.06178627 0.20597457 0.17655675 0.68702285 0.38410372 0.76984142
[7] 0.49769924 0.71761851 0.99190609 0.38003518
[[3]]
[1] 0.7774452 0.9347052 0.2121425 0.6516738 0.1255551
[[4]]
[1] 0.26722067 0.38611409 0.01339033 0.38238796 0.86969085 0.34034900
cat("Not Vectorized\n"); runif(n=c(10,10,5,6)) # doesn't show what I want
Not Vectorized
[1] 0.4820801 0.5995658 0.4935413 0.1862176
Note: outer()
performs outer product on arrays. Example
-
outer(1:3, 4:7) # default function performed = "*"
[,1] [,2] [,3] [,4]
[1,] 4 5 6 7
[2,] 8 10 12 14
[3,] 12 15 18 21
The function solve()
does the work -
mat2<- rbind(c(1,5),
c(9,5))
mat2
[,1] [,2]
[1,] 1 5
[2,] 9 5
solve(mat2)
[,1] [,2]
[1,] -0.125 0.125
[2,] 0.225 -0.025
mat2<- rbind(c(1,5),
c(9,5))
mat2*2
[,1] [,2]
[1,] 2 10
[2,] 18 10
mat2*5
[,1] [,2]
[1,] 5 25
[2,] 45 25
%*%
is used for matrix multiplication:
A<-rbind(c(2,10),
c(6,1))
A%*%A # matrix multiplication
[,1] [,2]
[1,] 64 30
[2,] 18 61
A%*%solve(A) # proof of A*A^(-1) = I
[,1] [,2]
[1,] 1 0
[2,] 0 1
A<-rbind(c(2,10,9),
c(6,1,5))
B<-rbind(c(1,5,2),
c(1,1,-4))
A-B # Subtraction
[,1] [,2] [,3]
[1,] 1 5 7
[2,] 5 0 9
A+B # addition
[,1] [,2] [,3]
[1,] 3 15 11
[2,] 7 2 1
A/B # division
[,1] [,2] [,3]
[1,] 2 2 4.50
[2,] 6 1 -1.25
Using the qr()
function for QR Decomposition of a Matrix
rank of a matrix can be obtained -
X <- matrix(c(1, 3, 9,
2, 4, 0,
3, 9, 27), ncol = 3, byrow = T)
qr(X)$rank
[1] 2
Let’s say we have a matrix X
of 3*3 -
Y <- matrix(c(4, 10, -5,
10, -8, 1,
2, 8, -3), nrow = 3, byrow = T)
Y
[,1] [,2] [,3]
[1,] 4 10 -5
[2,] 10 -8 1
[3,] 2 8 -3
To get the eigenvalues and eigenvectors use eigen()
-
eig <- eigen(Y)
eig
eigen() decomposition
$values
[1] -15.1863790 7.3231657 0.8632133
$vectors
[,1] [,2] [,3]
[1,] -0.5032161 0.7012153 0.2406474
[2,] 0.7579852 0.4913323 0.3726309
[3,] -0.4150084 0.5166137 0.8962338
To extract the values -
eig$values
[1] -15.1863790 7.3231657 0.8632133
To extract the eigen vectors -
eig$vectors
[,1] [,2] [,3]
[1,] -0.5032161 0.7012153 0.2406474
[2,] 0.7579852 0.4913323 0.3726309
[3,] -0.4150084 0.5166137 0.8962338
A square matrix \(A\) will be called diagonalizable if there exists an invertible matrix \(P\) such that \(P^{-1}AP\) is diagonal, then the matrix \(P\) is said to diagonalize matrix \(A\).
A = matrix(c(1,9,4,1), nrow = 2)
A
[,1] [,2]
[1,] 1 4
[2,] 9 1
Then the matrix \(P\) that diagonalizes \(A\) can is the matrix that contains the eigen vectors -
P = eigen(A)$vectors
P
[,1] [,2]
[1,] 0.5547002 -0.5547002
[2,] 0.8320503 0.8320503
So the diagonalized matrix will be -
d <- solve(P) %*% A %*% P
round(d, 2)
[,1] [,2]
[1,] 7 0
[2,] 0 -5
Notice that the diagonal elements are actually the eigen values -
eigen(A)$values
[1] 7 -5