矩陣是一個二維數(shù)組,只是(數(shù)值型、字符型或邏輯型)??赏ㄟ^函數(shù)matrix創(chuàng)建矩陣。一般使用格式為:
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,dimnames = NULL)
-data:包含了矩陣的元素;
-nrow和ncol:用以指定行和列的維數(shù);
-byrow:表明矩陣應當按行填充(byrow=TRUE)還是按列填充(byrow=FALSE),默認情況下按列填充
-dimnames:包含了可選的、以字符型向量表示的行名和列名;
1.創(chuàng)建矩陣
創(chuàng)建1:16的矩陣,行為4行,先按行排列
> mat = matrix(1:16,nrow = 4,ncol = 4,byrow = TRUE)
> mat
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
2. 查看矩陣維數(shù)并更改列名
dim(mat)
length(mat)
colnames(mat) = c('a','b','c','d')
rownames(mat) = c('e','f','g','h')
# 查看維度
> dim(mat)
[1] 4 4
# 查看元素總個數(shù)
> length(mat)
[1] 16
#更改列名
> colnames(mat) = c('a','b','c','d')
#更改行名
> rownames(mat) = c('e','f','g','h')
> mat
a b c d
e 1 2 3 4
f 5 6 7 8
g 9 10 11 12
h 13 14 15 16
> dim.name = dimnames(mat)
> dim.name
[[1]]
[1] "e" "f" "g" "h"
[[2]]
[1] "a" "b" "c" "d"
> dim.name[[1]]
[1] "e" "f" "g" "h"
3. 矩陣的取值
#取第一行,第三列元素
> mat[1,3]
[1] 3
#取第二行,所有列
> mat[2,]
a b c d
5 6 7 8
#取第三列到第四列,所有行
> mat[,3:4]
c d
e 3 4
f 7 8
g 11 12
h 15 16
#當取不相鄰矩陣的行和列可以用c()創(chuàng)建向量
> mat[c(1,3),c(2,4)]
b d
e 2 4
g 10 12
# 除去第一行 和第二列不取,其他全取
> mat[-1,-2]
a c d
f 5 7 8
g 9 11 12
h 13 15 16
#也可以使用行名和列名來取
> mat['f','a']
[1] 5
4. 矩陣的運算-元素間運算
對矩陣的每個元素進行加減乘除,且順序是按照列來算的。
#原始矩陣
> mat
a b c d
e 1 2 3 4
f 5 6 7 8
g 9 10 11 12
h 13 14 15 16
#每個元素乘以3
> mat*3
a b c d
e 3 6 9 12
f 15 18 21 24
g 27 30 33 36
h 39 42 45 48
#每一行均乘以1,2,3,4
> mat*c(1:4)
a b c d
e 1 2 3 4
f 10 12 14 16
g 27 30 33 36
h 52 56 60 64
#對矩陣運算不會改變原矩陣
> mat
a b c d
e 1 2 3 4
f 5 6 7 8
g 9 10 11 12
h 13 14 15 16
#如果需要保留矩陣運算之后的矩陣,需要額外命名
> mat1 = mat*c(1:4)
> mat1
a b c d
e 1 2 3 4
f 10 12 14 16
g 27 30 33 36
h 52 56 60 64
> mat*c(1:16) #按列進行對應元素相乘
a b c d
e 1 10 27 52
f 10 36 70 112
g 27 70 121 180
h 52 112 180 256
> mat + 5
a b c d
e 6 7 8 9
f 10 11 12 13
g 14 15 16 17
h 18 19 20 21
> mat/3
a b c d
e 0.3333333 0.6666667 1.000000 1.333333
f 1.6666667 2.0000000 2.333333 2.666667
g 3.0000000 3.3333333 3.666667 4.000000
h 4.3333333 4.6666667 5.000000 5.333333
> mat/c(1:3) #如果維度不對應,會有警告
a b c d
e 1.0 1 1.0 4
f 2.5 2 7.0 4
g 3.0 10 5.5 4
h 13.0 7 5.0 16
Warning message:
In mat/c(1:3) :
longer object length is not a multiple of shorter object length
5.矩陣運算-矩陣間運算
# 矩陣轉置
> t(mat)
e f g h
a 1 5 9 13
b 2 6 10 14
c 3 7 11 15
d 4 8 12 16
# 矩陣與矩陣相乘
> mat%*%mat
a b c d
e 90 100 110 120
f 202 228 254 280
g 314 356 398 440
h 426 484 542 600
> mat%*%c(1:4)
[,1]
e 30
f 70
g 110
h 150
#求矩陣的行列式
> det(mat)
[1] 4.733165e-30
#求矩陣的對角線元素
> diag(mat)
[1] 1 6 11 16
#rnorm表示在R中生成標準正態(tài)分布(normolisation)的隨機數(shù)
> mat2 = matrix(rnorm(16),nrow = 4)
> mat2
[,1] [,2] [,3] [,4]
[1,] 0.5741146 0.7455864 -0.2572695 -0.2411344
[2,] -0.5871949 -0.2625235 -0.1101282 0.7889012
[3,] 1.8245782 0.2449119 -0.6392340 -0.1252946
[4,] -0.2336952 0.2484867 -1.2900628 -0.3219892
#求逆
> mat3 = solve(mat2)
> mat3
[,1] [,2] [,3] [,4]
[1,] -0.1133153 -0.04712281 0.5378375 -0.2398813
[2,] 1.6648317 0.37849333 -0.4219256 -0.1552514
[3,] 0.2164346 -0.24894733 -0.2353717 -0.6804385
[4,] 0.4998786 1.32371081 0.2270614 -0.3251987
> mat2%*%mat3
[,1] [,2] [,3] [,4]
[1,] 1.000000e+00 0 -1.387779e-17 -2.775558e-17
[2,] -5.551115e-17 1 1.110223e-16 5.551115e-17
[3,] -1.387779e-17 0 1.000000e+00 0.000000e+00
[4,] 1.110223e-16 0 -1.387779e-17 1.000000e+00
#對行求均值
> rowMeans(mat)
e f g h
2.5 6.5 10.5 14.5
#對列求均值
> colMeans(mat)
a b c d
7 8 9 10
#對行求和
> rowSums(mat)
e f g h
10 26 42 58
#對列求和
> colSums(mat)
a b c d
28 32 36 40