Cody

Problem 1158. Sum the elements in either diagonal of a square matrix

Solution 2534154

Submitted on 13 Jun 2020 by Ramesh Kumar V
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
x = ones(2); y_correct = 4; assert(isequal(sumDiagonalElements(x),y_correct))

r = 2 c = 2 a = 1 1 b = 1 1 sum1 = 2 sum2 = 2 y = 4

2   Pass
x = ones(3); y_correct = 5; assert(isequal(sumDiagonalElements(x),y_correct))

r = 3 c = 3 a = 1 1 1 b = 1 1 1 sum1 = 3 sum2 = 3 y = 5

3   Pass
x = magic(8); y_correct = 520; assert(isequal(sumDiagonalElements(x),y_correct))

r = 8 c = 8 a = 64 55 46 37 28 19 10 1 b = 8 15 22 29 36 43 50 57 sum1 = 260 sum2 = 260 y = 520

4   Pass
x = magic(9); y_correct = 697; assert(isequal(sumDiagonalElements(x),y_correct))

r = 9 c = 9 a = 47 68 8 20 41 62 74 14 35 b = 37 38 39 40 41 42 43 44 45 sum1 = 369 sum2 = 369 y = 697

Suggested Problems

More from this Author2

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!