Matrix manipulation in R are very useful in Linear Algebra. Below are list of common yet important functions in dealing operations with matrices:
Transposing these, simply use
Multiplying these, would be
For the determinant, we have
Taking the inverse of
Finally, for eigenvalues and eigenvectors simply use
The output above returns the
- Transpose -
t
; - Multiplication -
%*%
; - Determinant -
det
; and, - Inverse -
solve
, orginv
of MASS library - Eigenvalues and Eigenvectors -
eigen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dat1 <- c(3,4,5,2,1,3,6,5,4) | |
matrix1 <- matrix(dat1, nrow = 3, ncol = 3, byrow = TRUE) | |
matrix1 | |
[,1] [,2] [,3] | |
[1,] 3 4 5 | |
[2,] 2 1 3 | |
[3,] 6 5 4 | |
dat2 <- c(6,7,5,4,5,8,7,6,6) | |
matrix2 <- matrix(dat2, nrow = 3, ncol = 3, byrow = TRUE) | |
matrix2 | |
[,1] [,2] [,3] | |
[1,] 6 7 5 | |
[2,] 4 5 8 | |
[3,] 7 6 6 |
t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t(matrix1) | |
[,1] [,2] [,3] | |
[1,] 3 2 6 | |
[2,] 4 1 5 | |
[3,] 5 3 4 | |
t(matrix2) | |
[,1] [,2] [,3] | |
[1,] 6 4 7 | |
[2,] 7 5 6 | |
[3,] 5 8 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix1 %*% matrix2 | |
[,1] [,2] [,3] | |
[1,] 69 71 77 | |
[2,] 37 37 36 | |
[3,] 84 91 94 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
det(matrix1) | |
[1] 27 | |
det(matrix2) | |
[1] 61 |
matrix1
is achieved by solve
or ginv
R functions. Note that ginv
is in MASS package, and so we have
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
solve(matrix1) | |
[,1] [,2] [,3] | |
[1,] -0.4074074 0.3333333 0.25925926 | |
[2,] 0.3703704 -0.6666667 0.03703704 | |
[3,] 0.1481481 0.3333333 -0.18518519 | |
library(MASS) | |
ginv(matrix1) | |
[,1] [,2] [,3] | |
[1,] -0.4074074 0.3333333 0.25925926 | |
[2,] 0.3703704 -0.6666667 0.03703704 | |
[3,] 0.1481481 0.3333333 -0.18518519 |
eigen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eigen(matrix1) | |
$values | |
[1] 11.238947 -2.088872 -1.150075 | |
$vectors | |
[,1] [,2] [,3] | |
[1,] -0.6017346 -0.3458537 -0.4307244 | |
[2,] -0.3305706 -0.5288292 0.8445517 | |
[3,] -0.7270754 0.7750644 -0.3181336 | |
eigen(matrix2) | |
$values | |
[1] 18.020246+0.000000i -0.510123+1.767726i -0.510123-1.767726i | |
$vectors | |
[,1] [,2] [,3] | |
[1,] 0.5729619+0i -0.4414068-0.3843531i -0.4414068+0.3843531i | |
[2,] 0.5495930+0i 0.6888097+0.0000000i 0.6888097+0.0000000i | |
[3,] 0.6079985+0i -0.2537249+0.3443799i -0.2537249-0.3443799i |
$values
, which is the eigenvalues, and $vectors
, the eigenvectors.