I have read documentation and the MATLAB community answers regarding this and I found a lot of helpful information about the power of sparse matrices, I just need help applying it to my sceanrio as this is the first time I am working with them.
I currently have a 22,400 x 22,400 sparse identity matrix that I created with speye(n) where n= 24,000. In a loop I have multiple element assignments that I do which iterates n number of times. The total run time for this code is 13.811054 seconds which is very poor considering this being a placeholder, and I plan on running 10e6 x 10e6 size matrices and I appreciate pointers to have more efficient code.
I ran multiple trials with on how the size affects my speed, along with time it took I calculated how much elements were allocated to the matrix (non-zero elements and I tabulated 3 readings below.
Size Time elapsed Allocation
576 x 576 0.128121 seconds 1.042%
2,800 x 2,800 0.657801 seconds 0.2143%
22,400 x 22,400 13.811054 seconds 0.0268%
The skeleton of my code is shown below;
A = -6.*speye(n);
for ix = 1:X
for iy = 1:Y
for iz = 1:Z
l wish to know what is the best to improve the efficency of this, I understand how clunky and inefficent my element allocation is and I want to improve it, any pointers are appreciated.
While I was reading answers on MATLAB, I came across John D'Errico's Answer to a similar question which dealt with using spalloc, which was well-worded, however I can't seem to understand how to apply spalloc() to my sceanrio where I already require a declared sparse identity matrix.
I hope to have an efficient code to run large matrix sizes, appreciate the help in advance.
**Edit - the 1s can be allocated only inside the nested loop as there are prior calculations that need to be done, I realize John D'Errico's Answer mentions in the beginning - In general, DON'T DO IT! That is, don't build a sparse matrix one element at a time. Even if you use spalloc, the matrix will be inefficient to build.
Edit - I am working with a symmetric banded matrix, the spine of it will be the speye() identity matrix