Problem 60850. Matrix Diagonal Sum
Given a square matrix A of size n x n, write a function to compute the sum of the elements on both the main diagonal and the anti-diagonal of the matrix.
- Main diagonal: The diagonal running from the top left to the bottom right.
- Anti-diagonal: The diagonal running from the top right to the bottom left.
If the matrix has an odd size, the center element (which lies on both diagonals) should only be counted once.
Solution Stats
Problem Comments
-
2 Comments
Christian Schröder
on 16 Apr 2025 at 6:46
Nice problem! But test cases 5 and 6 appear to be wrong: the answer should be 117, not 135.
Csaba Zoltán Kertész
on 16 Apr 2025 at 9:18
I agree, the answer on the last two test cases should be 117, which makes me wonder how can there be a correct solution to this problem
Solution Comments
Show commentsProblem Recent Solvers6
Suggested Problems
-
6 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!