你所在的位置: 首页 > 正文

R入门——矩阵

2020-03-14 点击:1427

建立矩阵

矩阵

v=1:6

m=矩阵(v,2,3) #默认加载

m

[,1] [,2] [,3]

[1,] 135

[2,] 246

n=矩阵(v,2,3,byrow=true) #加载

n

[,1] [,2] [,3]

[1,] 1 2 3

[2,]4 5 6

dim(m)# matrix dimension

[1]2 3

matrix行和列命名

rownames (m)=c ('mal ',' femal ')#命名row

colnames (m)=c ('low ',' middle ', 高))#命名列

dimnames(m)#查看矩阵的行和列名

[[1]]

[1]"男性" "女性"

[[2]]

[1]"低" "中" "高"

m #参见矩阵M

低中高

男性1535

女性246

单位矩阵

x=Diag (3)

[,1] [,2] [,3]

[,1] [,2] [,3]

[1,]100

[2,]0 10

[3, ] 0 0 1

对角矩阵

y=diag(m)

y-矩阵(1:4,2,2)

[1] 1 4

由m对角矩阵的对角元素组成

z=diag (v)

[5,]0 0 0 0 5 0

[,1] [,2] [,3] [,4] [,5] [,6]

[1,]1 00 00 00 00

[2,]0 2 0 0 0 0

[3,]0 0 3 0 0 0

[4,]0 0 0 4 0

由m对角矩阵的对角元素组成

0 ]0,000,000 6

矩阵的下标(index)和子集(元素)的抽取

规则:同一向量的下标

生成3行4列矩阵

z=矩阵(1:12,3,4)

Z

[,1] [,2] [,3] [,4]

[1,] 1,4710

[2,] 2,5811

[3,] 3 6 9 12

抽取矩阵元素

z[2,4]] c (2,4)] ]

Extract Column 2

Extract Column 2

z[,2]

[1]4 5 6

Delete matrix element

z[,-C (1,3)]

[,1] [,2]

[2,] 2,5811

[3,] 3 6 9 12

抽取矩阵元素

[3,] 6 12

将第4列替换为缺失值

Z[,4]=na

由m对角矩阵的对角元素组成

[1][,2] [,3] [,4]

[1,]147 na

[2,] 258 nana (z)]=1

[,1] [,2] [,3] [,4]

由m对角矩阵的对角元素组成

[2,] 2 5 8 1

[3,] 3 6 9 1

矩阵运算(函数)

1。转置t ()

1。转置t ()

x=矩阵(1:6,2,3)

[,1] [,2] [,3]

[,1] [,2] [,3]

[,1] [,2] [,3]

[1,] 135

[2,] 246

[,1] [,2]

[2,] 2,5811

[2,]34

[3,] 56

几种矩阵组合

对角矩阵

[2,] 2,5811

[,1] [,2]

[1,]13

1要求相同的行数

cbind (x,y)

[,1] [,2] [,3] [,4] [,5]

[1,] 1 35 13

[2,] 2 46 24

按行合并,要求相同的列数

由m对角矩阵的对角元素组成

[,1] [,2] [,3]

[,1] [,2] [,3]

[1,]1-2-3

[,1] [,2] [,3]

[1,] 135

[2,] 246

[1,] 135

[2,] 246

[3,] 1-2-3

[,1] [,2] [,3]

[,1] [,2] [,3]

[1,] 135

[2,] 246

由m对角矩阵的对角元素组成

[,1] [,2] [,3]

[,1] [,2] [,3]

[1,]1-2-3

[,1] [,2] [,3]

[1,]1-2-3

[,1] [,2] [,3]

[2,] 4-5-6

乘以相应的值 ] -8 -20 -36

双矩阵代数积

x% *% t (z)

[2,] 2,5811

[1,] 22-49

[2,] 28-64

双矩阵代数积

tcrossprod (x,z)

[2,] 2,5811

[1,] 22-49

[2,] 28-64

=t (x)% *% z

横杆(x,z)

[,1] [,2] [,3]

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]

[1,]-1-2-3-3-6-9-5-10-15

[2,]-4-5-6-12-15-18-20-25-30

[3,]-2-4-6-4-8-12-6-12-18

[4,]8-8

对角矩阵

[2,] 2,5811

[,1] [,2]

[1,]13

返回值是一个包含值和向量的列表

ev=特征值(y)

ev

特征值()分解

$ values

[1]5.-0.

$ vectors

[,1] [,2]

[1,]-0.-0.

[2,]-0..

ev $ values取出特征值(λ1,λ2,)

ev $向量取出相应的特征向量(v1,v2,)

因为这些代码都被打乱了。因此,只有TUT

矩阵的奇异值分解

A=UDV才能被捕获,其中U'U=V'V=I,D是对角矩阵

返回一个列表

a

[,1] [,2] [,3] [,4]

[1,] 211

[2,] 121

[3,]1 1 2 1

[4,]1 1 2

矩阵A的行列式 b)

authentication

[1]-101 2

[,1]

[1]-1.4-0 . 40 . 61 . 6

authentication

a % * % x

[,1]

[1,]1.

[2,] 2.e-16

[3,]1.

[4,] 2.e 00

a逆矩阵inva

inva=solve (a)

[,1] [,2] [,3] [,4]

[, ] -0.2 -0.2 0.8 -0.2

[4,] -0.2 -0.2 -0.2 0.8

[,1] [,2] [,3] [,4]

[,1] [,2] [,3] [,4]

[,1] [,2] [,3] [,4]

[2,]-8. e-17. e 00-17 矩阵

m=1:12

m=矩阵(m,3,4)

Centered Center=t

[,1] [,2] [,3] [,4]

[,1] [,2] [,3] [,4]

[2,] 2 58 11

[3,] 3 69 12

求每行的平均值

MARGIN=1表示一行。 fun=均值求平均值

apply (m,margin=1,FUN=均值)

[1]5.56.57.5

求每列的中值

MARGIN=2表示列,FUN=中值求中值

apply(m,MARGIN=2,FUN=中值)

[1]2 5 8 11

Centered by Column

Centered Center=t

[,1] [,2] [,3] [,4]

[1,]-1-1-1-1

[,1] [,2] [,3] [,4]

[1,] 1-1-1 ] 0 0 0

[3,]1 11 1

attr(" scaled : center ")

[1]2 5 8 11

验证每列平均值

apply (m cen,margin=2,fun=mean)

[1]0 0 0

standard by column

center=T,scale=T

m.stand - scale(m,center=T,scale=T)

ROW。MED

[,1] [,2] [,3] [,4]

[1,]-1-1-1-1

[,1] [,2] [,3] [,4]

[2 " scale : center ")

[1]2 5 8 11

attr(" " scale : scale ")

[1]1 11 1

center=T,scale=T

apply(m . stand,margin=2,FUN=mean)

[1]0 0 0 0

apply(m . stand,MARGIN=2,FUN=sd)

后续数据帧建立

Find每列的中值

ROW。 MED-APPLY (M,边距=1,FUN=中值)

ROW。MED

[,1] [,2] [,3] [,4]

[2 " scale : center ")

[1]2 5 8 11

attr(" " scale : scale ")

[1]1 11 1

center=T,scale=T

[3,] 4.5-1.51.54.5

后续数据帧建立

[2,] 2.e-16

apply(m . stand,MARGIN=2,FUN=sd)

后续数据帧建立

apply (m cen,margin=2,fun=mean)

验证每列平均值

[,1] [,2] [,3] [,4]

仙女山新闻网 版权所有© www.lyhuwei.com 技术支持:仙女山新闻网 | 网站地图