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
Solution Comments
Show comments
Loading...
Problem Recent Solvers11
Suggested Problems
-
11 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Nice problem! But test cases 5 and 6 appear to be wrong: the answer should be 117, not 135.
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