sumDiag
Efficient and compact code for summing each diagonal (or anti-diagonal) in a matrix without using a for-loop.
Works well for large matrices.
For 3D matrix input A, the sum of diagonals of A(:,:,k) are returned in
sumMat(:,k). The script is typically faster than a for-loop based approach when A is 3D.
The code is most efficient for wide or tall matrices. Inline the code when it is used as a part of an iterative algorithm, to avoid recomputing constant indexing matrices.
Note that a for-loop implementation with the diag() function can be faster, and has a lower memory requirement, especially in the square 2D case.
Cite As
Marcus Björk (2024). sumDiag (https://www.mathworks.com/matlabcentral/fileexchange/44162-sumdiag), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.