Speeding up sparse indexing
3 ビュー (過去 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 件)
Walter Roberson
2019 年 5 月 11 日
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!