Matrix Operations
[This article was first published on Analysis with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Matrix manipulation in R are very useful in Linear Algebra. Below are lists of common yet important functions in dealing operations with matrices:Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
- Transpose – t
- Multiplication – %*%
- Determinant – det
- Inverse – solve, or ginv 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 |
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 |
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 |
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 |
More about matrix here.
To leave a comment for the author, please follow the link and comment on their blog: Analysis with R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.