matrix manipulation of a large diagonally symmetric matrix to reduce computational time
Show older comments
Hi there, Given a large square matrix (maybe up to 20k X 20k). I would like to reduce computational time for calculating special functions (elliptic integrals). When I image up this matrix it is diagonally symmetric as shown below. This diagonal symmetric behaviour is as expected I've shown 3 cases below.
What is the most efficient approach to reduce computational time for a matrix with this symmetry? Perhaps remove half the matrix then do computation then replicate using repmat? If this can be done it will greatly reduce the computational time. Any suggestions will be very helpful
Thanks
case 1

case 2

case 3

7 Comments
James Tursa
on 23 Apr 2015
If you decide to create a new variable with only the lower (or upper) triangle to work with, that means data copies in both directions. While this can be done at the m-file level, you may end up with large intermediate matrices in the process. This will slow things down and potentially create memory problems given your stated 20K x 20K size. These large intermediate variables can be avoided with a fairly simple mex routine. Do you have a C compiler available?
Udit Gupta
on 23 Apr 2015
Are you doing an element by element operation or a matrix operation?
Joseph
on 30 Apr 2015
Joseph
on 30 Apr 2015
Edited: James Tursa
on 30 Apr 2015
James Tursa
on 30 Apr 2015
Edited: James Tursa
on 30 Apr 2015
Since you are creating the matrices from scratch with the zeros function, I have some suggestions:
1) Use the uninit function from the FEX to pre-allocate ... it is much faster than the zeros function for large matrices. You can find uninit here:
2) Do the operations in-place in a mex routine, which will prevent large intermediate copies. (I can do this coding for you)
I can work on the mex routine to do this over the next few days, but I have a question about the last three lines.
MNTxNT(logical(eye(size(MNTxNT)))) = 0;
If the diagonal starts as 0's because of the zeros pre-allocation, and you don't set the diagonal to anything in your loops (j index starts at i+1), why do you go through the trouble of setting it to 0's explicitly? It should already be 0's. Also, even if you insist on doing this, a much more efficient way of setting the diagonal to 0's would have been:
MNTxNT(1:NT+1:end) = 0;
And then these lines:
k2ij(k2ij(:)==0) = []; % remove zero elements
MNTxNT(MNTxNT(:)==0) = []; % remove zero elements
You are physically removing the 0 elements, which will turn the matrices into vectors (i.e., extracting the triangle). Is this what you want for downstream programming beyond the ellipke function? Or are you going to turn them to full again later on? If you really want them this way, then better to just build them that way from the beginning in the mex routine. Let me know how you really want this so I can start coding.
Basically, what explicitly do you want as variables after all of this is finished? Back to full square symmetic matrices? Knowing this will determine how the mex routines are coded.
Joseph
on 30 Apr 2015
Answers (0)
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!