我想测试R 4.1.1中矩阵的对称性.我有一个数据帧,然后我把它转换成一个矩阵.带有class()
函数的数据类型现在返回"matrix"
和"array"
.矩阵是对称的,但函数isSymmetric()
返回FALSE
.
这就是我所做的:
## Sample dataframe
mat1=data.frame(one=c(64,1,2,0),two=c(1,0,0,0),three=c(2,0,6,45),four=c(0,0,45,140))
## now we convert it to matrix
mat2 = as.matrix(mat1)
class(mat2)
## Note how this will return FALSE, even when its symmetric
isSymmetric(mat2)
## Now I turn the matrix into a vector and convert it again.
mat3 = matrix(as.numeric(mat2),
nrow = dim(mat2)[1])
class(mat3)
## Now this works:
isSymmetric(mat4)
我的问题是:除了熔化矩阵并再次构建它之外,还有更直接的方法来判断矩阵的对称性吗?我需要阅读许多大型矩阵,这是一种非常复杂的处理方法.