
R语言是一门非常方便的数据分析语言,它内置了许多处理矩阵的方法。下面列出一些常用的矩阵操作方法示例。
矩阵的生成
> mat <- matrix(1:16, ncol = 4, nrow = 4, byrow=TRUE, dimnames=list(c(paste("x", 1:4, sep = ".")), c(paste("y", 1:4, sep = "."))))
> mat
y.1 y.2 y.3 y.4
x.1 1 2 3 4
x.2 5 6 7 8
x.3 9 10 11 12
x.4 13 14 15 16
# 矩阵的行列名还可以使用rownames或者colnames进行修改
> rownames(mat) <- paste("row", 1:4, sep=".")
> colnames(mat) <- paste("col", 1:4, sep=".")
> mat
col.1 col.2 col.3 col.4
row.1 1 2 3 4
row.2 5 6 7 8
row.3 9 10 11 12
row.4 13 14 15 16
矩阵的维度
# 表示这是一个4行4列的矩阵
> dim(mat)
[1] 4 4
矩阵的加减
> mat.2 <- matrix(51:66, nrow = 4)
> mat.2
[,1] [,2] [,3] [,4]
[1,] 51 55 59 63
[2,] 52 56 60 64
[3,] 53 57 61 65
[4,] 54 58 62 66
> mat.plus <- mat + mat.2
> mat.plus
col.1 col.2 col.3 col.4
row.1 52 57 62 67
row.2 57 62 67 72
row.3 62 67 72 77
row.4 67 72 77 82
> mat.minus <- mat - mat.2
> mat.minus
col.1 col.2 col.3 col.4
row.1 -50 -53 -56 -59
row.2 -47 -50 -53 -56
row.3 -44 -47 -50 -53
row.4 -41 -44 -47 -50
矩阵的转置
> t(mat)
row.1 row.2 row.3 row.4
col.1 1 5 9 13
col.2 2 6 10 14
col.3 3 7 11 15
col.4 4 8 12 16
矩阵相乘
# 若A矩阵的维度为m*n,那么B矩阵的维度应为n*p
# 生成的结果矩阵的维度为m*p
> mat.3 <- matrix(1:8, nrow = 4)
> mat.3
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
> mat.mcl <- mat %*% mat.3
> mat.mcl
[,1] [,2]
row.1 30 70
row.2 70 174
row.3 110 278
row.4 150 382
返回矩阵的对角
> diag(mat)
[1] 1 6 11 16
生成上三角或下三角矩阵
# 生成上三角矩阵,注意这里的diag参数如果为TRUE,
# 表示把矩阵对角也包括进来。设置为FALSE就是不包括。
> mat[!upper.tri(mat, diag = TRUE)] <- 0
> mat
col.1 col.2 col.3 col.4
row.1 1 2 3 4
row.2 0 6 7 8
row.3 0 0 11 12
row.4 0 0 0 16
# 生成下三角矩阵
> mat[!lower.tri(mat, diag = TRUE)] <- 0
> mat
col.1 col.2 col.3 col.4
row.1 1 0 0 0
row.2 5 6 0 0
row.3 9 10 11 0
row.4 13 14 15 16
求解逆矩阵
> solve(mat)
row.1 row.2 row.3 row.4
col.1 1.00000000 6.832142e-17 0.00000000 0.0000
col.2 -0.83333333 1.666667e-01 0.00000000 0.0000
col.3 -0.06060606 -1.515152e-01 0.09090909 0.0000
col.4 -0.02651515 -3.787879e-03 -0.08522727 0.0625
求行列式的值
> det(mat)
[1] 4.733165e-30
矩阵的特征值和特征向量
> mat.e <- eigen(mat)
> mat.e
eigen() decomposition
$values
[1] 3.620937e+01 -2.209373e+00 -3.188632e-15 -1.348401e-16
$vectors
[,1] [,2] [,3] [,4]
[1,] -0.1511543 0.7270500 0.5037002 -0.06456091
[2,] -0.3492373 0.2832088 -0.8319577 -0.31932112
[3,] -0.5473203 -0.1606324 0.1528148 0.83232496
[4,] -0.7454033 -0.6044736 0.1754427 -0.44844294
# 可以使用mat.e$values和mat.e$vectors取出结果
奇异值分解
> svd(mat)
$d
[1] 3.862266e+01 2.071323e+00 7.609772e-16 3.860638e-16
$u
[,1] [,2] [,3] [,4]
[1,] -0.1347221 -0.82574206 0.3812474 -0.39325613
[2,] -0.3407577 -0.42881720 -0.2152141 0.80850658
[3,] -0.5467933 -0.03189234 -0.7133141 -0.43724476
[4,] -0.7528288 0.36503251 0.5472808 0.02199431
$v
[,1] [,2] [,3] [,4]
[1,] -0.4284124 0.7186535 0.2825595 -0.4692122
[2,] -0.4743725 0.2738078 -0.7264762 0.4150089
[3,] -0.5203326 -0.1710379 0.6052738 0.5776189
[4,] -0.5662928 -0.6158835 -0.1613571 -0.5234156
QR分解
> qr(mat)
$qr
col.1 col.2 col.3 col.4
row.1 -16.6132477 -18.2986497 -1.998405e+01 -2.166945e+01
row.2 0.3009646 -1.0767638 -2.153528e+00 -3.230291e+00
row.3 0.5417363 -0.3456506 1.350645e-15 3.139336e-15
row.4 0.7825080 -0.9120325 1.643990e-01 7.300782e-17
$rank
[1] 2
$qraux
[1] 1.060193e+00 1.220731e+00 1.986394e+00 7.300782e-17
$pivot
[1] 1 2 3 4
attr(,"class")
[1] "qr"
> qr.Q(qr(mat))
[,1] [,2] [,3] [,4]
[1,] -0.06019293 -0.83449195 -0.3906679 -0.3838992
[2,] -0.30096463 -0.45762462 0.2347489 0.8030523
[3,] -0.54173634 -0.08075729 0.7025058 -0.4544068
[4,] -0.78250805 0.29611005 -0.5465868 0.0352538
> qr.R(qr(mat))
col.1 col.2 col.3 col.4
row.1 -16.61325 -18.298650 -1.998405e+01 -2.166945e+01
row.2 0.00000 -1.076764 -2.153528e+00 -3.230291e+00
row.3 0.00000 0.000000 1.350645e-15 3.139336e-15
row.4 0.00000 0.000000 0.000000e+00 7.300782e-17