稀疏的矩阵尺寸与常规矩阵大小
我的常规矩阵具有对象大小416字节,当我使用为(,“ sparsematrix”)
将其变成稀疏矩阵时,此稀疏矩阵的大小将达到1720字节。
正常吗?我们不应该期望稀疏矩阵比常规矩阵更小的存储尺寸吗?
非常感谢!
My regular matrix has object size 416 bytes, and when I use as(, "sparseMatrix")
to turn it into sparse matrix, then the size for this sparse matrix goes up to 1720 bytes.
Is it normal? Shouldn't we expect a smaller storage size for the sparse matrix than the regular one?
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
矩阵
是R的基本数据结构之一,并且可以用很少的元数据存储:它是一个值序列,每个维度仅为每个维度和数据类型。sparsematrix
对象中包含更多的元数据,因为您将在下面的示例中看到str()
。最突出的是,对于每个非零值AN(x,y)位置除了值本身外,还存储了位置。如果您存储整数,则仅此一项就会导致记忆使用的三倍。仅当有许多零值时,这才能得到补偿,因为它们根本没有存储。密集的示例
比较矩阵的零值:
或这样的矩阵的较大版本:
稀疏示例
在这里我们创建一个更稀疏的矩阵,只有6个零,只有3个值。我们可以看到Sparsematrix仅存储3个值。
最后,Sparsematrix可以节省预期的内存:
matrix
is one of the base data structures of R, and can be stored with very little metadata: it is a sequence of values with just a length for each dimension, and a data type.A
sparseMatrix
object however contains more metadata, as you'll see withstr()
in the examples below. Most prominently, for each non-zero value an (x,y) position is stored in addition to the value itself. This alone will cause a threefold increase in memory use, if you're storing integers. This is only compensated when there are many zero values, as they are not stored at all.Dense example
Compare for a matrix with no zero values:
Or a larger version of such a matrix:
Sparse example
Here we create a more sparse matrix, with 6 zeroes and only 3 values. We can see that the sparseMatrix only stores the 3 values.
And finally a case where the sparseMatrix gives the expected memory savings: