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 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 5 Nov 2014
Modifié(e) : MathWorks Support Team
le 25 Fév 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?
Réponse acceptée
MathWorks Support Team
le 25 Fév 2021
Modifié(e) : MathWorks Support Team
le 25 Fév 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Sparse Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!