官术网_书友最值得收藏!

Matrix functions

Furthermore, there are specific functions for matrices:

  • The following function extracts the diagonal as a vector:
m<-matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3)
m
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
diag(m)
## [1] 1 5 9
  • Returns the dimensions of a matrix:
dim(m)
## [1] 3 3
  • Returns the sum of columns of a matrix:
colSums(m)
## [1] 6 15 24
  • Returns the sum of rows of a matrix:
rowSums(m)
## [1] 12 15 18
  • The transpose of a matrix can be obtained using the following code:
t(m)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
  • Returns the determinant of a matrix:
det(m)
## [1] 0
  • The auto-values and auto-vectors of a matrix are obtained using the following code:
eigen(m)
## eigen() decomposition
## $values
## [1] 1.611684e+01 -1.116844e+00 -5.700691e-16
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.4645473 -0.8829060 0.4082483
## [2,] -0.5707955 -0.2395204 -0.8164966
## [3,] -0.6770438 0.4038651 0.4082483
主站蜘蛛池模板: 临沧市| 建阳市| 富民县| 伊宁市| 盘锦市| 门头沟区| 临江市| 宕昌县| 团风县| 海门市| 晋中市| 邵东县| 新河县| 万宁市| 邯郸市| 会同县| 鄂温| 嘉鱼县| 陆良县| 孟连| 苍梧县| 赣榆县| 东乌珠穆沁旗| 舞钢市| 沧州市| 长岭县| 扬中市| 普兰店市| 宣恩县| 泸西县| 城口县| 资阳市| 弥渡县| 石渠县| 孟村| 红原县| 灵宝市| 武夷山市| 宁明县| 扬州市| 望都县|