Matrix Algebra in R
Vijay Kumar
Department of Mathematics and Statistics
DDU Gorakhpur University, Gorakhpur
vkgkp@[Link]
March 05, 2024
Introduction Functions Eigen Ax = b Decomposition Examples Definition Combining Matrices
Matrix Algebra in R
Defining a Matrix in R
Entering by Columns/Rows
Entering a Column or Row Vector
Extracting Pieces of a Matrix
Extracting Individual Elements
Extracting a Row/Column of a Matrix
Extracting Several Rows and/or Columns
Combining Matrices
Joining Rows
Joining Columns
Basic Matrix Operations
Matrix Addition and Subtraction
Scalar Multiplication
Matrix Multiplication
Matrix Transposition
Matrix Inversion
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 2 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Definition Combining Matrices
Defining a Matrix in R
Entering a Matrix By Row:
We can enter the numbers by row, simply by adding an optional
input variable
1 2
A=
3 4
The R commands are:
> A <- matrix(c(1 ,2 ,3 ,4) , nrow=2 , ncol=2 , byrow = TRUE )
>A
[,1] [,2]
[1,] 1 2
[2,] 3 4
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 3 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Definition Combining Matrices
Accessing Elements of Vectors or Matrices
Let A be a single row array. To access the ith element of A type A[i].
Let A be an nxn matrix. To access the (i, j)th element type A[i,j].
0.1 0.4 0.3 0.2
0.2 0.1 0.4 0.3
A=
0.3 0.2 0.1 0.4
A <- matrix(c(0.1, 0.4, 0.3, 0.2,
0.4 0.3 0.2 0.1
Here are the R commands: 0.2, 0.1, 0.4, 0.3,
>A[ ,1] # 1st column 0.3, 0.2, 0.1, 0.4,
[1] 0.1 0.2 0.3 0.4 0.4, 0.3, 0.2, 0.1),
> A[1, ] # 1st row nrow=4, ncol=4, byrow=T)
[1] 0.1 0.4 0.3 0.2
> A[2,3] # 2nd element of 3rd column
[1] 0.4
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 4 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Definition Combining Matrices
Combining Matrices
Joining Rows:
On occasion, we need to build up matrices from Smaller parts. We can
combine several matrices with the same number of columns by joining
them as rows, using the rbind()command
rbind(A, B) : creates a matrix with the vector/matrix A as its first row
and B as its second row
Joining Columns:
In similar fashion, we can combine several matrices with the same
number of rows by joining them as columns, using the cbind()
command
cbind(A, B) : creates a matrix with the vector/matrix A as its first
column and B as its second column
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 5 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Definition Combining Matrices
Combining Matrices : rbind()
> A <- matrix(c (1 ,3 ,3 ,9 ,6 ,5) ,2 ,3)
> B <- matrix(c (9 ,8 ,8 ,2 ,9 ,0) ,2 ,3)
>A >B
[,1] [,2] [,3] [,1] [,2] [,3]
[1,] 1 3 6 [1,] 9 8 9
[2,] 3 9 5 [2,] 8 2 0
> rbind(A,B) > rbind(B,A)
[,1] [,2] [,3] [,1] [,2] [,3]
[1,] 1 3 6 [1,] 9 8 9
[2,] 3 9 5 [2,] 8 2 0
[3,] 9 8 9 [3,] 1 3 6
[4,] 8 2 0 [4,] 3 9 5
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 6 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Definition Combining Matrices
Combining Matrices : cbind()
> A <- matrix(c (1 ,3 ,3 ,9 ,6 ,5) ,2 ,3)
> B <- matrix(c (9 ,8 ,8 ,2 ,9 ,0) ,2 ,3)
>A >B
[,1] [,2] [,3] [,1] [,2] [,3]
[1,] 1 3 6 [1,] 9 8 9
[2,] 3 9 5 [2,] 8 2 0
> cbind(A,B)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 6 9 8 9
[2,] 3 9 5 8 2 0
> cbind(B,A)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 9 8 9 1 3 6
[2,] 8 2 0 3 9 5
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 7 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Addition & Subtraction Multiplication Transpose Inverse
Basic Matrix Manipulation
Matrix Functions
diag(A) extract the diagonal of the matrix A
diag(v) diagonal matrix with elements in the vector v
diag(n) the n x n identity matrix
det(A) determinant of A
t(A) matrix transpose
A+B matrix addition (An×n and Bn×n )
A-B matrix subtraction (An×n and Bn×n )
A %*% B matrix product of Am×n and Bn×p
Linear Equations
solve(A) inverse the square matrix A
solve(A, b) solution of system of linear equations
Decompositions
chol(A) the Choleski decomposition
eigen(A) eigenvalues and eigenvectors
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 8 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Addition & Subtraction Multiplication Transpose Inverse
Matrix Addition and Subtraction
Adding or subtracting matrices is natural and straightforward.
> A <- matrix(c (1 , 3 , 3 , 9) , 2 , 2)
> B <- matrix(c (9 , 8 , 8 , 2) , 2 , 2)
>A > A+B
[,1] [,2] [,1] [,2]
[1,] 1 3 [1,] 10 11
[2,] 3 9 [2,] 11 11
>B > A-B
[,1] [,2] [,1] [,2]
[1,] 9 8 [1,] -8 -5
[2,] 8 2 [2,] -5 7
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 9 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Addition & Subtraction Multiplication Transpose Inverse
Matrix Multiplication
> A <- matrix(c(5, 3, 2, 1), nrow=2, ncol=2, byrow=T)
> B <- matrix(c(2, 5, 8, 4), nrow=2, ncol=2, byrow=T)
>A > A%*%B # AB
[,1] [,2] [,1] [,2]
[1,] 5 3 [1,] 34 37
[2,] 2 1 [2,] 12 14
> B%*%A # BA
>B
[,1] [,2]
[,1] [,2]
[1,] 20 11
[1,] 2 5
[2,] 48 28
[2,] 8 4
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 10 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Addition & Subtraction Multiplication Transpose Inverse
Matrix Transposition
> A <- matrix(c(5, 3, 2, 1),nrow=2, ncol=2, byrow=T)
> B <- matrix(c(9, 8, 9, 8, 2, 0),nrow=2, ncol=3, byrow=T)
>A > t(B) # transpose of B
[,1] [,2] [,1] [,2]
[1,] 5 3 [1,] 9 8
[2,] 2 1 [2,] 8 2
> det(A) # determinant of A [,3] 9 0
[1,] -1
>B > t(t(B))
[,1] [,2] [,3]
[,1] [,2] [,3]
[1,] 9 8 9
[1,] 9 8 9
[2,] 8 2 0 [2,] 8 2 0
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 11 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Addition & Subtraction Multiplication Transpose Inverse
Matrix Inversion
>A <- matrix(c(0.1, 0.4, 0.3, 0.2,0.2, 0.1, 0.4, 0.3,0.3, 0.2, 0.1, 0.4,0.4, 0.3, 0.2, 0.1),
nrow=4, ncol=4, byrow=T)
>Ainv <- solve(A) # Inverse of A
> Ainv 0.1 0.4 0.3 0.2
[,1] [,2] [,3] [,4] 0.2 0.1 0.4 0.3
A=
[1,] -2.25 0.25 0.25 2.75 0.3 0.2 0.1 0.4
[2,] 2.75 -2.25 0.25 0.25
[3,] 0.25 2.75 -2.25 0.25 0.4 0.3 0.2 0.1
[4,] 0.25 0.25 2.75 -2.25
> Ainv%*%A # verification
[,1] [,2] [,3] [,4]
[1,] 1.000000e+00 -8.348357e-18 1.193707e-16 5.968533e-17
[2,] -3.330534e-17 1.000000e+00 -9.991939e-17 -8.326334e-18
[3,] -9.159475e-17 -8.604499e-17 1.000000e+00 -1.790238e-16
[4,] 1.498910e-16 9.161508e-17 7.494548e-17 1.000000e+00
> #This is numerically close to the identity matrix.
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 12 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen Values and Eigen Vectors:
Let us take an example of a 3 × 3 matrix to illustrate some properties of eigenvalues
and eigenvectors.
We could consider this to be the variance-covariance matrix of three variables, but
the main thing is that the matrix is square and symmetric, which guarantees that the
eigenvalues, λi are real numbers.
Covariance matrices are also positive semi-definite, meaning that their eigenvalues are
non-negative, λi ≥ 0.
A <- matrix(c(13, -4, 2, -4, 11, -2, 2, -2, 8), 3, 3, byrow=TRUE)
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 13 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen contd...:
Get the eigenvalues and eigenvectors using eigen(); this returns a named list, with
eigenvalues named values and eigenvectors named vectors.
ev <- eigen(A)
# extract components
values <- ev$values
vectors <- ev$vectors
The eigenvalues are always returned in decreasing order, and each column of vectors
corresponds to the elements in values.
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 14 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen contd...:
A <- matrix(c(13, -4, 2, -4, 11, -2, 2, -2, 8), 3, 3, byrow=TRUE)
> ev <- eigen(A)
> # extract components
> values <- ev$values
> vectors <- ev$vectors
> values
[1] 17 8 7
> vectors
[,1] [,2] [,3]
[1,] 0.7453560 0.6666667 0.0000000
[2,] -0.5962848 0.6666667 0.4472136
[3,] 0.2981424 -0.3333333 0.8944272
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 15 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen contd...:
Orthogonality: Eigenvectors are always orthogonal, V ′ V = I
P
trace(A) = sum of eigenvalues, λi .
P 2
sum of squares of A = sum of squares of eigenvalues, λi
Q
determinant = product of eigenvalues, det(A) = λi . This means that the determinant
will be zero if any λi = 0.
rank = number of non-zero eigenvalues
eigenvalues of A−1 = 1/eigenvalues of A. The eigenvectors are the same, except for
order, because eigenvalues are returned in decreasing order.
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 16 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen contd...:
Get the eigenvalues and eigenvectors using eigen(); this returns a named list, with
eigenvalues named values and eigenvectors named vectors.
We call these L and V here, but in formulas they correspond to a diagonal matrix,
Λ = diag(λ1 , λ2 , λ3 ), and a (orthogonal) matrix V.
ev <- eigen(A)
# extract components
L <- ev$values
V <- ev$vectors
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 17 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen contd...:
Factorization: of A
A : A = Vdiag(L)V′ .
That is, the matrix A can be represented as the product A = VΛV′ .
V %*% diag(L) %*% t(V)
Diagonalization : V diagonalizes A
L = V′ AV.
That is, the matrix V transforms A into the diagonal matrix Λ, corresponding to orthogonal
(uncorrelated) variables.
diag(L)
zapsmall(t(V) %*% A %*% V)
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 18 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Eigen Values & Vectors Factorization & Diagonalization
Eigen contd...
> eigen(A)
$values
[1] 1.0+0.0i -0.2+0.2i -0.2-0.2i -0.2+0.0i A <- matrix(c(0.1, 0.4, 0.3, 0.2,
0.2, 0.1, 0.4, 0.3,
0.3, 0.2, 0.1, 0.4,
$vectors 0.4, 0.3, 0.2, 0.1),
nrow=4, ncol=4, byrow=T)
[,1] [,2] [,3] [,4]
[1,] -0.5+0i -0.5+0.0i -0.5-0.0i -0.5+0i
[2,] -0.5+0i 0.0-0.5i 0.0+0.5i 0.5+0i
[3,] -0.5+0i 0.5+0.0i 0.5+0.0i -0.5+0i
[4,] -0.5+0i 0.0+0.5i 0.0-0.5i 0.5+0i
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 19 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Linear equation:Ax = b
A is an m × n matrix of coefficients for m equations in n unknowns
x is an n × 1 vector unknowns, x1 , x2 , . . . xn
b is an m × 1 vector of constants, the ”right-hand sides” of the equations
The general conditions for solutions are:
the equations are consistent (solutions exist) if r(A|b) = r(A)
the solution is unique if r(A|b) = r(A) = n
the solution is underdetermined if r(A|b) = r(A) < n
the equations are inconsistent (no solutions) if r(A|b) > r(A)
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 20 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Solving AX=b
0.1 0.4 0.3 0.2 x1 1
0.2 0.1 0.4 0.3 x 2
A= X = 2 b=
0.3 0.2 0.1 0.4 x3 3
0.4 0.3 0.2 0.1 x4 1
> solve(A,b) R script :
A <- matrix(c(0.1, 0.4, 0.3, 0.2,
[,1] 0.2, 0.1, 0.4, 0.3,
[1,] 1.75 0.3, 0.2, 0.1, 0.4,
[2,] -0.75 0.4, 0.3, 0.2, 0.1),
[3,] -0.75 nrow=4, ncol=4, byrow=T)
b <- matrix(c(1, 2, 3, 1), nrow=4,
[4,] 6.75 ncol=1, byrow=F)
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 21 / 27
Introduction Functions Eigen Ax = b Decomposition Examples Choleski
Choleski Factorization : chol()
chol(A): Compute the Choleski factorization of a real symmetric positive-
definite square matrix. The upper triangular factor of the Choleski
decomposition, i.e., the matrix R such that R'R =A
>A <- matrix(c(25, -2, 4, -2, 4, 1, 4, 1, 9), nrow=3, ncol=3, byrow=T)
>R <- chol(A)
>R
[,1] [,2] [,3] 25 −2 4
[1,] 5 -0.400000 0.8000000
[2,] 0 1.959592 0.6736097 A = −2 4 1
[3,] 0 0.000000 2.8118055 4 1 9
> t(R)%*%R
[,1] [,2] [,3]
[1,] 25 -2 4
[2,] -2 4 1
[3,] 4 1 9
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 22 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Example : AX = b
Solve the following system of linear equations :
2x1 + x2 + x3 − 2x4 = −10, 4x1 + 2x3 + x4 = 8,
3x1 + 2x2 + 2x3 = 7, x1 + 3x2 + 2x3 − x4 = −5
The system of equations may be written in matrix form AX = b, as
2 1 1 −2 x1 −10
4 0 2 1 x2 8
=
3 2 2 0 x3 7
1 3 2 −1 x4 −5
# R Code
A <- matrix(c(2,1,1,-2,4,0,2,1,3,2,2,0,1,3,2,-1), 4, 4, byrow=T)
b <- c(-10, 8, 7, -5)
solve(A, b)
# 5 6 -10 8
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 23 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Example
(i) x + y + 2z = 4, 3x + y − 3z = 4, 2x − 3y − 5z = −5
A <- matrix(c(1,1,2,3,1,-3,2,-3,-5), 3, 3, byrow=T)
b <- c(4, 4, -5)
solve(A, b)
# 1.2962963 1.6666667 0.5185185
(ii) x + y + z = 10, 2x + y + 2z = 17, 3x + 2y + z = 17
A <- matrix(c(1,1,1,2,1,2,3,2,1), 3, 3, byrow=T)
b <- c(10, 17, 17)
solve(A, b)
# 2 3 5
(iii) 2x + 3y − z = 5, 4x + 4y − 3z = 3, 2x − 3y + 2z = 2
A <- matrix(c(2,3,-1,4,4,-3,2,-3,2), 3, 3, byrow=T)
b <- c(5, 3, 2)
solve(A, b)
# 1 2 3
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 24 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Examples contd...
(iv) 2x + 4y + z = 2 3x + 2y − 2z = −2 x − y + z = 6
A <- matrix(c(2,4,1,3,2,-2,1,-1,1), 3, 3, byrow=T)
b <- c(2, -2, 6)
solve(A, b)
# 2.0 -1.2 2.8
(v) 5x + 2y + z = 4 7x + y − 5z = 8 3x + 7y − 4z = 10
A <- matrix(c(5,2,1,7,1,-5,3,7,-4), 3, 3, byrow=T)
b <- c(4, 8, 10)
solve(A, b)
# 0.5903084 0.8281938 -0.6079295
(vi) 2x + 2y + z = 12, 3x + 2y + 2z = 8, 5x + 10y − 8z = 10
A <- matrix(c(2,2,1,3,2,2,5,10,-8), 3, 3, byrow=T)
b <- c(12, 8, 10)
solve(A, b)
# -12.750 14.375 8.750
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 25 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Examples contd...
Solve the following system of linear equations:
1 2x − 3y + z = −1, x + 4y + 5z = 25, 3x − 4y + z = 2
2 10x + y + z = 12, x + 10y + z = 12, x + y + 10z = 12
3 2x − 3y + z = −1, x + 4y + 5z = 25, 3x − 4y + z = 2
4 5x − 2y + z = 4, 7x + y − 5z = 8, 3x + 7y + 4z = 10
5 5x − y + z = 10, 2x + 4y = 12, x + y + 5z = −1
6 5x + 2y + z = 12, x + 4y + 2z = 15, x + 2y + 5z = 20
7 10x + 2y + z = 9, 2x + 20y − 2z = −14, −2x + 3y + 10z = 22
8 x + y + z = 3, 2x − y + 3z = 16, 3x + y − z = −3
9 10x + y + z = 12, 2x + 10y + z = 13, 2x + 2y + 10z = 14
10 2x − y = 0, −x + 2y − z = 0 − y + 2z − u = 0 − z + 2u = 1
11 x1 + 2x2 + 3x3 = 14, 2x1 + 5x2 + 2x3 = 18, 3x1 + x2 + 5x3 = 20
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 26 / 27
Introduction Functions Eigen Ax = b Decomposition Examples
Examples contd...
′ ′ ′
Verify (i) (AB)−1 = B −1 A−1 (ii) (AB) = B A
Compute inverse, eigen values and vectors of the following matrices.
2 1 1 −2 2 1 −4 1
4 0 2 1 −4 3 5 −2
(i) D = , (ii) H =
1 −1
3 2 2 0 1 −2
1 3 2 −1 1 3 −3 2
1 1 1 1 2 4 1 2 3
(iii) A = 2 −1 3 , (iv) B = 4 1 1 (v) C = 2 2 8
3 1 −1 3 2 2 3 7 4
V Kumar, DDU Gorakhpur University R Programming: Matrix in R March 2024 27 / 27