Cody

# Problem 2585. How to reverse spdiags ?

Solution 1941232

Submitted on 18 Sep 2019
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 = spdiags([1 2 3;4 5 6;7 8 9]); assert(isequal(spdiags(inv_spdiags(x)),x))

start = 3 1 places = 3 1 m = 0 0 0 0 0 0 7 0 0 start = 1 3 places = 1 3 m = 0 0 3 0 0 0 7 0 0 start = 2 1 places = 2 3 1 2 m = 0 0 3 4 0 0 7 8 0 start = 1 2 places = 1 2 2 3 m = 0 2 3 4 0 6 7 8 0 start = 1 1 places = 1 2 3 1 2 3 m = 1 2 3 4 5 6 7 8 9 start = 1 1 places = 1 2 3 1 2 3 m = 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

2   Fail
b = spdiags(eye(10)); assert(isequal(spdiags(inv_spdiags(b)),b))

start = 10 1 places = 10 1

Error using sub2ind (line 55) Out of range subscript. Error in inv_spdiags (line 10) m(sub2ind([r c],places(1,:),places(2,:)))=x(1:i,i) Error in Test2 (line 2) assert(isequal(spdiags(inv_spdiags(b)),b))

3   Fail
x = spdiags([1 2 3;4 5 6]); assert(isequal(spdiags(inv_spdiags(x)),x))

start = 2 1 places = 2 1 m = 0 0 0 0 0 0 start = 1 3 places = 1 3 m = 0 0 0 0 0 0 start = 1 1 places = 1 2 1 2 m = 1 0 0 0 5 0 start = 1 2 places = 1 2 2 3 m = 1 2 0 0 5 6 1 2 0 0 5 6

Assertion failed.

4   Pass
y = spdiags(magic(9)); assert(isequal(spdiags(inv_spdiags(y)),y))

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

5   Pass
x = spdiags(magic(5)); assert(isequal(spdiags(inv_spdiags(x)),x))

start = 5 1 places = 5 1 m = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 start = 1 5 places = 1 5 m = 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 start = 4 1 places = 4 5 1 2 m = 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 11 18 0 0 0 start = 1 4 places = 1 2 4 5 m = 0 0 0 8 15 0 0 0 0 16 0 0 0 0 0 10 0 0 0 0 11 18 0 0 0 start = 3 1 places = 3 4 5 1 2 3 m = 0 0 0 8 15 0 0 0 0 16 4 0 0 0 0 10 12 0 0 0 11 18 25 0 0 start = 1 3 places = 1 2 3 3 4 5 m = 0 0 1 8 15 0 0 0 14 16 4 0 0 0 22 10 12 0 0 0 11 18 25 0 0 start = 2 1 places = 2 3 4 5 1 2 3 4 m = 0 0 1 8 15 23 0 0 14 16 4 6 0 0 22 10 12 19 0 0 11 18 25 2 0 start = 1 2 places = 1 2 3 4 2 3 4 5 m = 0 24 1 8 15 23 0 7 14 16 4 6 0 20 22 10 12 19 0 3 11 18 25 2 0 start = 1 1 places = 1 2 3 4 5 1 2 3 4 5 m = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 start = 1 1 places = 1 2 3 4 5 1 2 3 4 5 m = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

6   Pass
z = spdiags(ones(10)); assert(isequal(spdiags(inv_spdiags(z)),z))

start = 10 1 places = 10 1 m = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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3 4 5 6 7 8 m = 0 0 0 ...

7   Pass
x = spdiags(randi(10,5)); assert(isequal(spdiags(inv_spdiags(x)),x))

start = 5 1 places = 5 1 m = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 start = 1 5 places = 1 5 m = 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 start = 4 1 places = 4 5 1 2 m = 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 7 2 0 0 0 start = 1 4 places = 1 2 4 5 m = 0 0 0 4 9 0 0 0 0 8 0 0 0 0 0 1 0 0 0 0 7 2 0 0 0 start = 3 1 places = 3 4 5 1 2 3 m = 0 0 0 4 9 0 0 0 0 8 4 0 0 0 0 1 1 0 0 0 7 2 6 0 0 start = 1 3 places = 1 2 3 3 4 5 m = 0 0 6 4 9 0 0 0 8 8 4 0 0 0 1 1 1 0 0 0 7 2 6 0 0 start = 2 1 places = 2 3 4 5 1 2 3 4 m = 0 0 6 4 9 2 0 0 8 8 4 7 0 0 1 1 1 2 0 0 7 2 6 7 0 start = 1 2 places = 1 2 3 4 2 3 4 5 m = 0 7 6 4 9 2 0 2 8 8 4 7 0 7 1 1 1 2 0 2 7 2 6 7 0 start = 1 1 places = 1 2 3 4 5 1 2 3 4 5 m = 3 7 6 4 9 2 3 2 8 8 4 7 5 7 1 1 1 2 3 2 7 2 6 7 1 start = 1 1 places = 1 2 3 4 5 1 2 3 4 5 m = 3 7 6 4 9 2 3 2 8 8 4 7 5 7 1 1 1 2 3 2 7 2 6 7 1 3 7 6 4 9 2 3 2 8 8 4 7 5 7 1 1 1 2 3 2 7 2 6 7 1

8   Fail
x = spdiags(randi(5,[3 2])); assert(isequal(spdiags(inv_spdiags(x)),x))

start = 2 1 places = 2 1 m = 0 0 0 2 0 0 start = 1 3 places = 1 3 m = 0 0 1 2 0 0 start = 1 1 places = 1 2 1 2 m = 5 0 1 2 4 0 start = 1 2 places = 1 2 2 3 m = 5 3 1 2 4 1 5 3 1 2 4 1

Assertion failed.

9   Fail
a = spdiags(zeros(5)); assert(isequal(spdiags(inv_spdiags(a)),a))

start = 5 1 places = 5 1

Index in position 2 exceeds array bounds. Error in inv_spdiags (line 10) m(sub2ind([r c],places(1,:),places(2,:)))=x(1:i,i) Error in Test9 (line 2) assert(isequal(spdiags(inv_spdiags(a)),a))