找回密码
 注册
查看: 25482|回复: 0

R语言矩阵转置的例子t()

[复制链接]
发表于 2011-3-3 22:39:26 | 显示全部楼层 |阅读模式
矩阵转置
A 为m×n 矩阵,求A'在R中可用函数t(),例如:
> A=matrix(1:12,nrow=3,ncol=4)
> A
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> t(A)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
若将函数t()作用于一个向量x,则R默认x为列向量,返回结果为一个行向量,例如:
> x
[1] 1 2 3 4 5 6 7 8 9 10
> t(x)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 2 3 4 5 6 7 8 9 10
> class(x)
[1] "integer"
> class(t(x))
[1] "matrix"
若想得到一个列向量,可用t(t(x)),例如:
> x
[1] 1 2 3 4 5 6 7 8 9 10
> t(t(x))
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
> y=t(t(x))
> t(t(y))
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2024-11-23 18:06 , Processed in 0.041173 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表