R语言包_Matrix

稀疏矩阵在数据稀疏的情况下节省了空间

library(Matrix)
m1 <- matrix(0, nrow = 1000, ncol = 1000)
m2 <- Matrix(0, nrow = 1000, ncol = 1000, sparse = TRUE)
m3 <- Matrix(0, nrow = 1000, ncol = 1000)
m4 <- Matrix(0, nrow = 1000, ncol = 1000, sparse = F)
object.size(m1)
# 8000200 bytes
object.size(m2)
# 5632 bytes
object.size(m3)
# 5632 bytes
object.size(m4)
# 9168 bytes

稀疏矩阵在矩阵不稀疏的情况下增大了空间

> m1 = matrix(1:6,nrow=3)
> m2 = Matrix(1:6,nrow=3)
> m3 = Matrix(1:6,nrow=3,sparse = T)
> m4 = Matrix(1:6,nrow=3,sparse = F)
> object.size(m1)
232 bytes
> object.size(m2)
1160 bytes
> m3 = Matrix(1:6,nrow=3,sparse = T)
> object.size(m3)
1512 bytes
> m4 = Matrix(1:6,nrow=3,sparse = F)
> object.size(m4)
1160 bytes

结论:矩阵稀疏的时候使用Matrix,矩阵不稀疏的情况下使用自带的matrix

参考资料

johnmyleswhite

你可能感兴趣的:(R语言包_Matrix)