MATLAB Answers

How to improve the speed of large matrix addition?

3 views (last 30 days)
Christian Stetco
Christian Stetco on 12 Apr 2021
Edited: James Tursa on 12 Apr 2021
Dear all,
I am currently working on a real-time implementation of a finite-element method solver which requires large matrix operations.
I encountered the following issue:
In my code, I need to perform addition of two matrices which are both M x M where M has minimum value of 5000. I know that the second matrix is a diagonal matrix.
So, for example code snippet like this
M = 5000;
A = randn(M, M);
B = eye(M);
C = A + B;
Unfortunately, this operation costs between 50 ms and 70 ms of speed, which is a lot in my application.
I also tried to only manipulate only the diagonal indices but in terms of timing there was no real improvement.
Is there any way of improving this in terms of speed?
Thanks and best regards,
Christian

Answers (1)

James Tursa
James Tursa on 12 Apr 2021
Edited: James Tursa on 12 Apr 2021
If you are generating a new matrix C each time, then you are essentially doing two things:
(1) Copying all the elements of A into a new matrix
(2) Adding the diagonals of B into this new matrix
So speeding up (2) isn't going to do anything to speed up (1). I.e., just indexing the diagonals may speed up (2) but all the data copying in step (1) is still going to cost you. If you could use just one matrix and avoid the data copying then maybe you might get a speed improvement:
A(1:M+1:end) = A(1:M+1:end) + B(1:M+1:end)
Or if you just held B as an M-element vector, then
A(1:M+1:end) = A(1:M+1:end) + B
The actual addition operation itself will not take much time at all. Just the normal MATLAB m-code overhead on this is going to cost you some time that you can't avoid, unless you do unapproved inplace operations via a mex routine.

Community Treasure Hunt

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

Start Hunting!