在矩阵中,如果数值为0的元素数目远远多于非0元素的数目,并且非0元素分布无规律时,则称该矩阵为稀疏矩阵(sparse matrix);与之相反,若非0元素数目占大多数时,则称该矩阵为稠密矩阵。
当一个矩阵中含有大量的0值时,可以将矩阵以稀疏矩阵的方式存储以解决资源。在R中,可以用Matrix这个包, 它可以将矩阵转化为稀疏矩阵。
#建立一个含大量0值的矩阵
M<-matrix(sample(c(0,0,1),size = 100,replace = T),10,10)
class(M)
M

#转化成稀疏矩阵,可以看到0变成了点
library(Matrix)
sparseM= Matrix(M)
class(sparseM)
sparseM

#转成普通的矩阵
as.matrix(sparseM)

网友评论