MATLAB Answers

Update a sparse matrix efficiently

31 views (last 30 days)
mafoEV on 5 Mar 2020
Commented: Bruno Luong on 25 Aug 2020
Dear MATLAB community,
I have a large sparse matrix (~ 100,000 by 100,000, with about 100,000 non-zero elements), which I need to update regularly (at each solver step).
So far, I've done it by creating its elements first (rows, columns and values) as column vectors, then assembling them using the sparse function:
A = sparse(iA, jA, vA, m, n, nnzA);
However this line is now taking 13% of my total solve time (called 355 times, taking about 0.29s each time).
The matrix structure does not change from one iteration to the next, only about half of the values need updating. Is there a more efficient way to do it? I haven't been able to find any solution so far looking at the forums.
I'm using Matlab 2019b Update 3 (
Many thanks.
  1 Comment
Bruno Luong
Bruno Luong on 25 Aug 2020
Are you updated only non-zero elements? only zero elements? Not known?
In three cases, the required time are differents.

Sign in to comment.

Answers (1)

sjhstone on 25 Aug 2020
You can refer to the following blog post. It seems that you have reached the optimal efficiency.
If the sparsity pattern does not change, and there are nonzero entries on the last column and the last row, I think it might be redundant to pass arguments nnzA. Because MATLAB seems to use MEX routine, we do need to do numerical experiments to see whether not passing them into sparse can accelerate your code.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by