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.
Speeding up sparse indexing
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!