0% found this document useful (0 votes)
31 views

RCourse Lecture8 Calculations

This document provides an introduction to basic matrix operations and functions in R software. It discusses how to multiply matrices by constants, perform matrix multiplication using the %*% operator, calculate cross products with crossprod(), add and subtract matrices, access rows and columns of matrices, calculate the inverse of a matrix using solve(), and find the eigenvectors and eigenvalues of a matrix using eigen(). The document uses examples to demonstrate how to apply these functions and operations to matrices in R.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

RCourse Lecture8 Calculations

This document provides an introduction to basic matrix operations and functions in R software. It discusses how to multiply matrices by constants, perform matrix multiplication using the %*% operator, calculate cross products with crossprod(), add and subtract matrices, access rows and columns of matrices, calculate the inverse of a matrix using solve(), and find the eigenvectors and eigenvalues of a matrix using eigen(). The document uses examples to demonstrate how to apply these functions and operations to matrices in R.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to R Software

Basics of Calculations
::::
Matrix Operations

Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur

1
Multiplication of a matrix with a constant
> x <- matrix(nrow=4, ncol=2, data=1:8, byrow=T )
> x
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8

2
Matrix multiplication: operator %*%

Consider the multiplication of X with X

> xtx <- t(x) %*% x


> xtx
[,1] [,2]
[1,] 84 100
[2,] 100 120

3
Cross product of a matrix X, XX, with a function crossprod

> xtx2 <- crossprod(x)


> xtx2
[,1] [,2]
[1,] 84 100
[2,] 100 120

Note: Command crossprod()executes the multiplication faster


than the conventional method with t(x)%*%x
4
Addition and subtraction of matrices (of same dimensions) can be
executed with the usual operators + and -
> x <- matrix(nrow=4, ncol=2, data=1:8, byrow=T)
> x
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8

5
Addition and subtraction of matrices (of same dimensions!) can be
executed with the usual operators + and -

> 4*x
[,1] [,2]
[1,] 4 8
[2,] 12 16
[3,] 20 24
[4,] 28 32

6
Addition and subtraction of matrices (of same dimensions!) can
be executed with the usual operators + and -
> x + 4*x
[,1] [,2]
[1,] 5 10
[2,] 15 20
[3,] 25 30
[4,] 35 40

> 4*x - x
[,1] [,2]
[1,] 3 6
[2,] 9 12
[3,] 15 18
[4,] 21 24
7
Access to rows, columns or submatrices:
> x <- matrix( nrow=5, ncol=3, byrow=T, data=1:15)
> x
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
[5,] 13 14 15

8
Access to rows, columns or submatrices:

> x[3,]
[1] 7 8 9

> x[,2]
[1] 2 5 8 11 14

> x[4:5, 2:3]


[,1] [,2]
[1,] 11 12
[2,] 14 15

9
Inverse of a matrix:

solve() finds the inverse of a positive definite matrix

Example:

> y<- matrix( nrow=2, ncol=2, byrow=T,


data=c(84,100,100,120))

> y
[,1] [,2]
[1,] 84 100
[2,] 100 120

> solve(y)
[,1] [,2]
[1,] 1.50 -1.25
[2,] -1.25 1.05 10
Eigen Values and Eigen Vectors:
eigen() finds the eigen values and eigen vectors of a positive
definite matrix

Example:
> y
[,1] [,2]
[1,] 84 100
[2,] 100 120

> eigen(y)
$values
[1] 203.6070864 0.3929136

$vectors
[,1] [,2]
[1,] 0.6414230 -0.7671874
[2,] 0.7671874 0.6414230 11

You might also like