Cody

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

Solution 3822463

Submitted on 24 Nov 2020
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))

x = 1 1 1 1 y = 4

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

x = 1 1 1 1 1 1 1 1 1 y = 6

Assertion failed.

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

x = 64 9 17 40 32 41 49 8 2 55 47 26 34 23 15 58 3 54 46 27 35 22 14 59 61 12 20 37 29 44 52 5 60 13 21 36 28 45 53 4 6 51 43 30 38 19 11 62 7 50 42 31 39 18 10 63 57 16 24 33 25 48 56 1 y = 520

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

x = 47 57 67 77 6 16 26 36 37 58 68 78 7 17 27 28 38 48 69 79 8 18 19 29 39 49 59 80 9 10 20 30 40 50 60 70 1 11 21 31 41 51 61 71 81 12 22 32 42 52 62 72 73 2 23 33 43 53 63 64 74 3 13 34 44 54 55 65 75 4 14 24 45 46 56 66 76 5 15 25 35 y = 738

Assertion failed.

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!