MATLAB中带有spfun的稀疏矩阵乘法 [英] Sparse matrix multiplication in MATLAB with spfun
问题描述
我有一个大小为(m,1)
的密集列矩阵y
和一个大小为(m,n)
的稀疏矩阵x
.
我想使用y
和x
的每一列进行按元素的乘法.
所得的稀疏矩阵的大小仍为(m,n)
.
加载到内存中的稀疏矩阵x
约为10GB.
spfun
可以帮助我以记忆有效的方式实现目标吗?
I have a dense column matrix y
of size (m,1)
and a sparse matrix x
of size (m,n)
.
I want to do element-wise multiplication using y
and every column of x
.
The resultant sparse matrix is still of size (m,n)
.
Sparse matrix x
, when loaded into memory, is about 10GB.
Can spfun
help me accomplish my goal in a memory efficient manner?
我很难理解其背后的逻辑.
I am having difficulties understanding the logic behind it.
谢谢.
推荐答案
您是否尝试过 bsxfun ?
out = bsxfun( @times, x, y );
spfun
更适用于元素明智的操作,其中您可以操纵x
的每个非零元素.它不完全适合矩阵矢量元素明智的操作.
但是,如果您想沿这条路线做某事,则可以尝试:
spfun
is more suitable for element-wise operations where you manipulate each non-zero element of x
. It is not exactly fit for matrix-vector element wise operations.
However, if you want to do something along this line, you might try:
[ii jj xij] = find(x); %// extract non-zeros of x and their locations
out = sparse( ii, jj, xij.*y(ii), size(x,1), size(x,2) );
有关更多信息,请参见 doc find
.
See doc find
for more information.
这篇关于MATLAB中带有spfun的稀疏矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!