Cody

# Problem 545. Flip the main diagonal of a matrix

Solution 1898867

Submitted on 13 Aug 2019 by Yuumi Ozaki
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
M=magic(5); y_correct = [9, 24,1,8 ,15; 23 ,21,7,14,16; ... 4,6,13,20,22; 10,12,19,5,3; 11,18,25,2,17]; assert(isequal(flipDiagonal(M),y_correct))

r = 5 c = 5 dia = 17 5 13 21 9 j = 5 k = 5 x = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 17 j = 4 k = 4 x = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 5 3 11 18 25 2 17 j = 3 k = 3 x = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 5 3 11 18 25 2 17 j = 2 k = 2 x = 17 24 1 8 15 23 21 7 14 16 4 6 13 20 22 10 12 19 5 3 11 18 25 2 17 j = 1 k = 1 x = 9 24 1 8 15 23 21 7 14 16 4 6 13 20 22 10 12 19 5 3 11 18 25 2 17 j = 0 k = 0 y = 9 24 1 8 15 23 21 7 14 16 4 6 13 20 22 10 12 19 5 3 11 18 25 2 17

2   Pass
M=magic(2); y_correct=[2 3; 4 1]; assert(isequal(flipDiagonal(M),y_correct))

r = 2 c = 2 dia = 1 2 j = 2 k = 2 x = 1 3 4 1 j = 1 k = 1 x = 2 3 4 1 j = 0 k = 0 y = 2 3 4 1

3   Pass
M=ones(4); M(2,2)=4; M(3,3)=9; M(4,4)=16; y_correct=ones(4); y_correct(1,1)=16; y_correct(2,2)=9; y_correct(3,3)=4; assert(isequal(flipDiagonal(M),y_correct))

r = 4 c = 4 dia = 1 4 9 16 j = 4 k = 4 x = 1 1 1 1 1 4 1 1 1 1 9 1 1 1 1 1 j = 3 k = 3 x = 1 1 1 1 1 4 1 1 1 1 4 1 1 1 1 1 j = 2 k = 2 x = 1 1 1 1 1 9 1 1 1 1 4 1 1 1 1 1 j = 1 k = 1 x = 16 1 1 1 1 9 1 1 1 1 4 1 1 1 1 1 j = 0 k = 0 y = 16 1 1 1 1 9 1 1 1 1 4 1 1 1 1 1

4   Pass
M=[1]; y_correct=[1]; assert(isequal(flipDiagonal(M),y_correct))

r = 1 c = 1 dia = 1 j = 1 k = 1 x = 1 j = 0 k = 0 y = 1

5   Pass
M=reshape(1:25,[5,5]); y_correct=[25 2 3 4 5; 6 19 8 9 10; ... 11 12 13 14 15; 16 17 18 7 20; 21 22 23 24 1]'; assert(isequal(flipDiagonal(M),y_correct))

r = 5 c = 5 dia = 1 7 13 19 25 j = 5 k = 5 x = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 1 j = 4 k = 4 x = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 7 24 5 10 15 20 1 j = 3 k = 3 x = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 7 24 5 10 15 20 1 j = 2 k = 2 x = 1 6 11 16 21 2 19 12 17 22 3 8 13 18 23 4 9 14 7 24 5 10 15 20 1 j = 1 k = 1 x = 25 6 11 16 21 2 19 12 17 22 3 8 13 18 23 4 9 14 7 24 5 10 15 20 1 j = 0 k = 0 y = 25 6 11 16 21 2 19 12 17 22 3 8 13 18 23 4 9 14 7 24 5 10 15 20 1

6   Pass
M=[1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4]; y_correct=[4,1,1,1;2,3,2,2;3,3,2,3;4,4,4,1]; assert(isequal(flipDiagonal(M),y_correct))

r = 4 c = 4 dia = 1 2 3 4 j = 4 k = 4 x = 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 1 j = 3 k = 3 x = 1 1 1 1 2 2 2 2 3 3 2 3 4 4 4 1 j = 2 k = 2 x = 1 1 1 1 2 3 2 2 3 3 2 3 4 4 4 1 j = 1 k = 1 x = 4 1 1 1 2 3 2 2 3 3 2 3 4 4 4 1 j = 0 k = 0 y = 4 1 1 1 2 3 2 2 3 3 2 3 4 4 4 1

### Community Treasure Hunt

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

Start Hunting!