I am trying to convert a dense matrix into a sparse matrix to speed up my matrix multiplies, but it is running slowly. How can I fix this?
8 views (last 30 days)
Show older comments
MathWorks Support Team
on 5 Nov 2014
Edited: MathWorks Support Team
on 25 Feb 2021
I have constructed a large matrix A that has very few non-zero entries, and I would like to make use of sparse matrix multiplication so that my code runs faster. However, I noticed that when I run
>> y = sparse(A)*x;
the execution is very slow. How can I improve the performance of my code?
Accepted Answer
MathWorks Support Team
on 25 Feb 2021
Edited: MathWorks Support Team
on 25 Feb 2021
The computational bottleneck in this code comes from converting an existing dense matrix into a sparse matrix. If the matrix has only a few non-zero entries, sparse matrix multiplication will indeed be much more efficient. However, there may be a large overhead cost in constructing the sparse matrix from the dense matrix that exists. The cost of the conversion process may therefore negate the savings gained in the multiplication.The best option is to construct the matrix A to be sparse from the beginning, rather than building it as a dense matrix and converting it:
>> A = sparse(i, j, s, m, n);
This will provide the performance gain of the sparse multiplication without the overhead cost of converting a dense matrix into a sparse matrix. However, if it s not possible to do so, then it could prove more efficient to simply perform a dense matrix multiplication.
0 Comments
More Answers (0)
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!