Sie verfolgen jetzt diese Einreichung
- Aktualisierungen können Sie in Ihrem Feed verfolgter Inhalte sehen.
- Je nach Ihren Kommunikationseinstellungen können Sie auch E-Mails erhalten.
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.
Zitieren als
Marcus Björk (2026). sumDiag (https://de.mathworks.com/matlabcentral/fileexchange/44162-sumdiag), MATLAB Central File Exchange. Abgerufen .
Allgemeine Informationen
- Version 1.4.0.0 (1,59 KB)
Kompatibilität der MATLAB-Version
- Kompatibel mit allen Versionen
Plattform-Kompatibilität
- Windows
- macOS
- Linux
| Version | Veröffentlicht | Versionshinweise | Action |
|---|---|---|---|
| 1.4.0.0 | Added support for 3D matrices as input. |
||
| 1.1.0.0 | Added comment on the speed of the implementation compared to using a for-loop approach. |
||
| 1.0.0.0 |
