Speeding up sparse indexing
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following sparse diagonal matrix that I am generating for a system in IxJxK dimensions:
A = sparse(I*J*K, I*J*K);
for k = 1:K
A((k-1)*I*J+1: k*I*J, (k-1)*I*J+1: k*I*J) = Ai{k};
end
where Ai is a cell with with K (I*K x I*K) sparse diagonal matrices. That is, I am just iteratively inserting diagonals from the Ai sparse matrices into my "master matrix" A. I've tried to insert the elements of Ai directly instead of saving them in a cell first. This does not really change the speed. Is there any way to do this more efficiently?
Thanks in advance :-)
0 commentaires
Réponses (1)
Walter Roberson
le 11 Mai 2019
When you make your initial sparse call, pass it all possible parameters to sparse. In particular you need the last one, the number of values that will be stored in the matrix, which you should set to the total number of values you will be inserting. If you do not use that then matlab will need to rebuild the sparse matrix every time you add more elements to it.
0 commentaires
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!