Speeding up sparse indexing
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Walter Roberson
el 11 de Mayo de 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 comentarios
Ver también
Categorías
Más información sobre Sparse Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!