Speeding up sparse indexing
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 11 May 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 Comments
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!