矩阵是一个按照长方阵列排列的复数或实数集合
向量是一维的,矩阵是二维的,需要行和列
R语言的矩阵是有维数的向量,矩阵元素可以是数值型,字符型或者逻辑型,每个元素必须拥有相同的模式
matrix(x,nrow,ncol,byrow)
x:表示矩阵中的个数
nrow:矩阵的行数
ncol:矩阵的列数
byrow:改变排序顺序;F:按列排列;T:按行排列(默认按列排序)
注意:矩阵中的元素数量必须是行跟列中元素数量的整倍数
x <- 1:20
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> matrix(x,4,5) #默认按列排序
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> matrix(x,4,5,T) #byrow=T,按行排序
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
> matrix(x,3,5) #20跟3不是倍数关系
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 13
[2,] 2 5 8 11 14
[3,] 3 6 9 12 15
Warning message:
In matrix(x, 3, 5) :
data length [20] is not a sub-multiple or multiple of the number of rows [3]
> matrix(x,5,3)
[,1] [,2] [,3]
[1,] 1 6 11
[2,] 2 7 12
[3,] 3 8 13
[4,] 4 9 14
[5,] 5 10 15
Warning message:
In matrix(x, 5, 3) :
data length [20] is not a sub-multiple or multiple of the number of columns [3]
> matrix(x,5,5) #行跟列的乘积>矩阵的数量
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 1
[2,] 2 7 12 17 2
[3,] 3 8 13 18 3
[4,] 4 9 14 19 4
[5,] 5 10 15 20 5
m <- matrix(x,4,5)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
#给行跟列命名
> rname <- c("R1","R2","R3","R4")
> rname
[1] "R1" "R2" "R3" "R4"
> cname <- c("C1","C2","C3","C4","C5")
> cname
[1] "C1" "C2" "C3" "C4" "C5"
#将名字赋值给矩阵
> dimnames(m) <- list(rname,cname)
> m
C1 C2 C3 C4 C5
R1 1 5 9 13 17
R2 2 6 10 14 18
R3 3 7 11 15 19
R4 4 8 12 16 20
> m
C1 C2 C3 C4 C5
R1 1 5 9 13 17
R2 2 6 10 14 18
R3 3 7 11 15 19
R4 4 8 12 16 20
> dim(m)
[1] 4 5
#n是一个向量,没有维数
> n <- 1:20
> dim(n)
NULL
#给矩阵添加维数,变成一个矩阵
> dim(n) <- c(4,5)
> n
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> dim(n)
[1] 4 5
>
- 访问矩阵指定位置的数据
- 访问矩阵的行
- 访问矩阵的列
- 访问矩阵对应的行
- 访问矩阵指定行跟列的子矩阵
- 访问矩阵中去除了某个数字后的行/列
> m <- matrix(1:10,4,5)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 3 7
[2,] 2 6 10 4 8
[3,] 3 7 1 5 9
[4,] 4 8 2 6 10
> m[2,3]
[1] 10
> m[2,]
[1] 2 6 10 4 8
> m[,3]
[1] 9 10 1 2
> m[2]
[1] 2
> m[c(2,3),c(3,4)]
[,1] [,2]
[1,] 10 4
[2,] 1 5
> m[-2,4]
[1] 3 5 6
> m[2,-4]
[1] 2 6 10 8
- 通过名称来访问矩阵
> rname <- c("r1","r2","r3","r4")
> cname <- c("c1","c2","c3","c4","c5")
> dimnames(m) <- list(rname,cname)
> m
c1 c2 c3 c4 c5
r1 1 5 9 3 7
r2 2 6 10 4 8
r3 3 7 1 5 9
r4 4 8 2 6 10
> m["r1","c2"]
[1] 5
> m[,"c2"]
r1 r2 r3 r4
5 6 7 8
> m["r3",]
c1 c2 c3 c4 c5
3 7 1 5 9
> m[c("r1","r3"),c("c2","c4")]
c2 c4
r1 5 3
r3 7 5
矩阵的乘法
内积:对应位置元素的相乘,x * y
外积:矩阵x的第一行乘以矩阵y的第二行,x %*% y
列数求和:colSums()函数
行数求和:rowSum()函数
列数求平均数:colMeans()函数
行数求平均数:rowMeans()函数
矩阵转置:t()函数
对角矩阵对角线的值
> m <-matrix(1:8,4,4)
> n <- matrix(11:18,4,4)
> m+n
[,1] [,2] [,3] [,4]
[1,] 12 20 12 20
[2,] 14 22 14 22
[3,] 16 24 16 24
[4,] 18 26 18 26
> n-m
[,1] [,2] [,3] [,4]
[1,] 10 10 10 10
[2,] 10 10 10 10
[3,] 10 10 10 10
[4,] 10 10 10 10
> m*n
[,1] [,2] [,3] [,4]
[1,] 11 75 11 75
[2,] 24 96 24 96
[3,] 39 119 39 119
[4,] 56 144 56 144
> m %*% n
[,1] [,2] [,3] [,4]
[1,] 154 202 154 202
[2,] 204 268 204 268
[3,] 254 334 254 334
[4,] 304 400 304 400
> colSums(m)
[1] 10 26 10 26
> m
[,1] [,2] [,3] [,4]
[1,] 1 5 1 5
[2,] 2 6 2 6
[3,] 3 7 3 7
[4,] 4 8 4 8
> t(m)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 1 2 3 4
[4,] 5 6 7 8
> diag(m)
[1] 1 6 3 8
数组至少是三维的
创建数组:
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> dim(x) <- c(2,3,4)
> x
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
array(data,dim,dimname)
data:是一个数值型的向量
dim:每个维度的长度
dimname:各个维度名称的标签,可选
> dim1 <- c('A1','A2')
> dim2 <- c('B1','B2','B3')
> dim3 <- c('C1','C2','C3','C4')
> z <- array(1:24,c(2,3,4),list(dim1,dim2,dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , C2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , C3
B1 B2 B3
A1 13 15 17
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24