Sparse matrix multiplication in MATLAB with spfun

主宰稳场 提交于 2019-12-13 00:49:02

问题


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.

Thank you.


回答1:


Have you tried bsxfun?

out = bsxfun( @times, x, y ); 

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) );

See doc find for more information.



来源:https://stackoverflow.com/questions/34627778/sparse-matrix-multiplication-in-matlab-with-spfun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!