0% found this document useful (0 votes)
33 views26 pages

R Matrices

This document provides examples of how to perform various matrix calculations in R, including adding, subtracting, multiplying matrices by scalars, taking the transpose, performing matrix multiplication, calculating the inner product of vectors, and more. It also demonstrates how to calculate eigenvalues and eigenvectors, perform singular value decompositions, and check for positive definiteness. Dimensional compatibility and matrix properties like rank, trace, and determinant are also explored.

Uploaded by

NawarajPokhrel
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)
33 views26 pages

R Matrices

This document provides examples of how to perform various matrix calculations in R, including adding, subtracting, multiplying matrices by scalars, taking the transpose, performing matrix multiplication, calculating the inner product of vectors, and more. It also demonstrates how to calculate eigenvalues and eigenvectors, perform singular value decompositions, and check for positive definiteness. Dimensional compatibility and matrix properties like rank, trace, and determinant are also explored.

Uploaded by

NawarajPokhrel
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/ 26

Matrix Calculation in R

Add and subtract matrices


> a = matrix(c(5, 2, 3, 1), nrow = 2, ncol = 2, byrow = T )
>a
[, 1] [, 2]
[1, ] 5
2
[2, ] 3
1
> b = matrix(c(8, 2, 5, 7), 2, 2, byrow = T )
>b
[, 1] [, 2]
[1, ] 8
2
[2, ] 5 7
>a+b
[, 1] [, 2]
[1, ] 13
4
[2, ] 2 6
>ab
[, 1] [, 2]
[1, ] 3
0
[2, ] 8
8

Multiplication by a scalar
> c = matrix(c(1, 2, 3, 0, 4, 5), 2, 2, byrow = T )
>c
[, 1] [, 2] [, 3]
[1, ] 1
2
3
[2, ] 0
4
5
>d=2c
[, 1] [, 2] [, 3]
4
6
> d [1, ] 2
[2, ] 0
8
10

Transpose of a matrix
> t(c)
[, 1]
[1, ] 1
[2, ] 2
[3, ] 3

[, 2]
0
4
5

Matrix multiplication
> a = matrix(c(3, 0, 2, 1, 1, 4), 2, 3, byrow = T )
>a
[, 1] [, 2] [, 3]
[1, ] 3
0
2
[2, ] 1
1
4
> b = matrix(c(1, 1, 1, 2, 1, 3), 3, 2, byrow = T )
>b
[, 1] [, 2]
[1, ] 1
1
[2, ] 1
2
[3, ] 1
3
> c = a% %b
>c
[, 1] [, 2]
[1, ] 1
3
[2, ] 4
11

Inner product
> x = c(1, 7, 6, 4)
> y = c(2, 2, 1, 5)
>x
[1] 1 7 6 4
>y
[1] 2 2 1 5
> t(x)% %y
[, 1]
[1, ] 2
> sum(x y)
[1] 2
> crossprod(x, y)
[, 1]
[1, ] 2

Order or dimension of a vector


> y = c(2, 2, 1, 5)
>y
[1] 2 2 1 5
> length(y)
[1] 4

Length of a vector
> ynorm = sqrt(crossprod(y, y))
> ynorm
[, 1]
[1, ] 5.830952

Elementwise multiplication
> a = matrix(c(3, 6, 2, 1), 2, 2, byrow = T )
>a
[, 1] [, 2]
[1, ] 3
6
[2, ] 2
1
> b = matrix(c(7, 4, 3, 2), 2, 2, byrow = T )
>b
[, 1] [, 2]
[1, ] 7
4
[2, ] 3
2
>ab
[, 1] [, 2]
[1, ] 21 24
[2, ] 6
2

Kronecker product
> a = matrix(c(2, 4, 0, 2, 3, 1), 3, 2, byrow = T )
>a
[, 1] [, 2]
[1, ] 2
4
[2, ] 0
2
[3, ] 3
1
> b = matrix(c(5, 3, 2, 1), 2, 2, byrow = T )
>b
[, 1] [, 2]
[1, ] 5
3
[2, ] 2
1
> kronecker(a, b)
[, 1] [, 2] [, 3] [, 4]
[1, ] 10
6
20
12
[2, ] 4
2
8
4
[3, ] 0
0 10 6
[4, ] 0
0
4 2
[5, ] 15
9
5 3
[6, ] 6
3
2 1

What happens when the dimensions of the matrices or


vectors are not appropriate for the operation
> a = matrix(c(1, 1, 1, 2), 2, 2, byrow = T )
> b = matrix(c(3, 0, 1, 1, 1, 4), 2, 3, byrow = T )
>a
[, 1] [, 2]
[1, ] 1
1
[2, ]
1
2
>b
[, 1] [, 2] [, 3]
[1, ]
3
0
1
[2, ]
1
1
4
>a+b
Error in a + b : non-conformable arrays

Create a diagonal matrix


> I3 = diag(rep(1, 3))
> I3
[, 1] [, 2] [, 3]
[1, ]
1
0
0
[2, ]
0
1
0
[3, ]
0
0
1

Trace of a matrix
> a = matrix(c(1, 3, 7, 2, 4, 8, 5, 6, 9), 3, 3, byrow = T )
>a
[, 1] [, 2] [, 3]
[1, ]
1
3
7
[2, ] 2
4
8
[3, ]
5
6
9
> tra = sum(diag(a))
> tra
[1]14

Inverse of a matrix
> ainv = solve(a)
> ainv
[r] [, 1] [, 2] [, 3]
[1, ]
6
7.5 2
[2, ] 11 13.0 3
[3, ]
4
4.5 1
> a% %ainv
[r]
[, 1]
[, 2]
[1, ] 1.000000e + 00
0
[2, ] 7.105427e 15
1
[3, ] 0.000000e + 00
0

[, 3]
1.776357e 15
1.776357e 15
1.000000e + 00

Rank of a matrix
> A = matrix(c(1, 1, 1, 2, 5, 1, 0, 1, 1), 3, 3, byrow = T )
>A
[, 1] [, 2] [, 3]
[1, ]
1
1
1
[2, ]
2
5
1
[3, ]
0
1
1
> qr(A)$rank
[1] 2

rank(A0 A) = rank(A)
> AtA = t(A)% %A
> AtA
[, 1] [, 2] [, 3]
[1, ]
5
11 1
[2, ] 11 27 5
[3, ] 1 5
3
> qr(AtA)$rank
[1] 2
> solve(AtA)
Error in solve.default(AtA) :
system is computationally singular: reciprocal condition number =
2.58191e-18

Row/column operations:
> sum(A)
[1] 9
> apply(A, 1, sum)
[1] 3 6 0
> apply(A, 2, sum)
[1] 3 7 1
> apply(A, 1, prod)
[1] 1 10 0
> apply(A, 1, mean)
[1] 1 2 0
> apply(A, 1, var)
[1] 0 9 1

Eigenvalues and Eigenvectors


> A = matrix(c(1, 2, 1, 4), 2, 2, byrow = T )
[, 1] [, 2]
2
> A [1, ] 1
[2, ] 1
4
> EA = eigen(A)
> EA
$values
[1] 3 2
$vectors
[, 1]
[1, ] 0.7071068
[2, ] 0.7071068

[, 2]
0.8944272
0.4472136

Singular Value Decomposition


> A = matrix(c(2, 0, 1, 1, 0, 2, 1, 1, 1, 1, 1, 1), 3, 4, byrow = T )
>A
[, 1] [, 2] [, 3] [, 4]
[1, ]
2
0
1
1
[2, ]
0
2
1
1
[3, ]
1
1
1
1
> svdA = svd(A)
> svdA
$d
[1]3.464102 2.000000 0.000000
$u
[, 1]
[1, ] 0.5773503
[2, ] 0.5773503
[3, ] 0.5773503

[, 2]
[, 3]
7.071068e 01 0.4082483
7.071068e 01 0.4082483
1.110223e 16 0.8164966

$v
[1, ]
[2, ]
[3, ]
[4, ]

[, 1]
0.5
0.5
0.5
0.5

[, 2]
[, 3]
7.071068e 01 0.3535534
7.071068e 01 0.3535534
1.110223e 16
0.8535534
1.110223e 16 0.1464466

> svdA$u% %t(svdA$u)


[, 1]
[, 2]
[, 3]
[1, ] 1.000000e + 00 8.326673e 17 1.110223e 16
[2, ] 8.326673e 17 1.000000e + 00 1.110223e 16
[3, ] 1.110223e 16 1.110223e 16 1.000000e + 00
> t(svdA$v)% %svdA$v
[, 1]
[, 2]
[, 3]
[1, ] 1.000000e + 00
1.110223e 16 1.387779e 17
[2, ] 1.110223e 16
1.000000e + 00 6.027326e 17
[3, ] 1.387779e 17 6.027326e 17 1.000000e + 00

> svdA$u% %diag(svdA$d)% %t(svdA$v)


[, 1]
[, 2]
[, 3]
[1, ] 2.000000e + 00 1.110223e 16
1
[2, ] 2.220446e 16 2.000000e + 00
1
[3, ] 1.000000e + 00
1.000000e + 00
1

[, 4]
1
1
1

> diag(svdA$d)% %diag(svdA$d)


[, 1] [, 2] [, 3]
[1, ] 12
0
0
[2, ]
0
4
0
[3, ]
0
0
0
> eigen(A% %t(A))$values
[1] 1.20000e + 01 4.00000e + 00 1.24345e 14
> eigen(t(A)% %A)$values
[1] 1.200000e + 01 4.000000e + 00 8.881784e 15

0.000000e + 00

Relationship between trace/determinanat and


eigenvalues
> B = matrix(c(1, 1, 2, 1, 2, 1, 0, 1, 1), 3, 3, byrow = T )
>B
[, 1] [, 2] [, 3]
[1, ]
1
1
2
[2, ] 1
2
1
[3, ]
0
1
1
> eigenB = eigen(B)
> eigenB
$values
[1] 2 1 1
$vectors
[, 1]
[, 2]
[, 3]
[1, ] 0.3015113 0.8017837 7.071068e 01
[2, ] 0.9045340 0.5345225 1.559729e 17
[3, ] 0.3015113 0.2672612 7.071068e 01

> traceB = sum(diag(B))


> traceB
[1] 2
> Re(sum(eigenB$values))
[1] 2
> detB = det(B)
> detB
[1] 2
> Re(prod(eigenB$values))
[1] 2

Eigenvalues of a square symmetric matrix


> A = matrix(c(4, 2, 1, 2, 4, 4, 1, 4, 8), 3, 3, byrow = T )
>A
[, 1] [, 2] [, 3]
[1, ]
4
2
1
[2, ]
2
4
4
[3, ] 1 4
8
> EA = eigen(A)
> EA
$values
[1] 11 4 1
$vectors
[, 1]
[, 2]
[1, ] 0.2672612 0.8728716
[2, ] 0.5345225 0.2182179
[3, ] 0.8017837 0.4364358

[, 3]
0.4082483
0.8164966
0.4082483

> SV DA = svd(A)
> SV DA
$d
[1] 11 4 1
$u
[, 1]
[, 2]
[1, ] 0.2672612 0.8728716
[2, ] 0.5345225 0.2182179
[3, ] 0.8017837 0.4364358
$v
[, 1]
[, 2]
[1, ] 0.2672612 0.8728716
[2, ] 0.5345225 0.2182179
[3, ] 0.8017837 0.4364358

[, 3]
0.4082483
0.8164966
0.4082483
[, 3]
0.4082483
0.8164966
0.4082483

An example of a square symmetric matrix that is not


positive definite
> W = matrix(c(4, 2, 1, 2, 6, 4, 1, 4, 9), 3, 3, byrow = T )
>W
[, 1] [, 2] [, 3]
[1, ]
4
2
1
[2, ]
2
6
4
[3, ] 1 4 9
> EW = eigen(W )
> EW
$values
[1] 8.151345 2.865783 10.017128
$vectors
[, 1]
[, 2]
[, 3]
[1, ] 0.4665008 0.88381658 0.0352886
[2, ] 0.8550024 0.46079428 0.2379907
[3, ] 0.2266009
0.08085101 0.9706262

> SV DW = svd(W )
> SV DW
$d
[1] 10.017128 8.151345 2.865783
$u
[, 1]
[, 2]
[, 3]
[1, ] 0.0352886 0.4665008 0.88381658
[2, ] 0.2379907 0.8550024 0.46079428
[3, ] 0.9706262 0.2266009
0.08085101
$v
[, 1]
[, 2]
[, 3]
[1, ] 0.0352886 0.4665008 0.88381658
[2, ] 0.2379907 0.8550024 0.46079428
[3, ] 0.9706262 0.2266009
0.08085101

Inverse of a matrix obtained by using the spectral


decomposition
> A = matrix(c(4, 2, 2, 6), 2, 2, byrow = T )
>A
[, 1] [, 2]
[1, ]
4
2
[2, ]
2
6
> Ainv = solve(A)
> Ainv
[, 1]
[, 2]
[1, ] 0.3 0.1
[2, ] 0.1 0.2
> Aev = eigen(A)$vectors
> Aeval = eigen(A)$values
> Ainv2 = Aev% %diag(1/Aeval)% %t(Aev)
> Ainv2
[, 1]
[, 2]
[1, ] 0.3 0.1
[2, ] 0.1 0.2

Solutions to linear equations


> x = c(3, 5)
>x
[1] 3 5
> b = solve(A, x)
>b
[1] 0.4 0.7

You might also like