Cody

# Problem 2585. How to reverse spdiags ?

Solution 1940583

Submitted on 17 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))

m = 0 0 0 0 0 0 0 0 0 start = 3 1 caselle = 3 1 m = 0 0 0 0 0 0 7 0 0 start = 1 3 caselle = 1 3 m = 0 0 3 0 0 0 7 0 0 start = 2 1 caselle = 2 3 1 2 m = 0 0 3 4 0 0 7 8 0 start = 1 2 caselle = 1 2 2 3 m = 0 2 3 4 0 6 7 8 0 start = 1 1 caselle = 1 2 3 1 2 3 m = 1 2 3 4 5 6 7 8 9 start = 1 1 caselle = 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))

m = 0×10 empty double matrix start = -8 1 caselle = -8 1

Error using sub2ind (line 43) Out of range subscript. Error in inv_spdiags (line 9) m(sub2ind([r,c],caselle(1,:),caselle(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))

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

Assertion failed.

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

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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 0 0 0 56 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 26 0 0 0 0 0 0 0 0 36 38 0 0 0 0 0 0 0 37 48 59 0 0 0 0 0 0 start = 6 1 caselle = 6 7 8 9 1 2 3 4 m = 0 0 0 0 0 0 23 34 45 0 0 0 0 0 0 0 44 46 0 0 0 0 0 0 0 0 56 0 0 0 0 0 0 0 0 0 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 = 1 6 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 ...

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

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 start = 5 1 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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 caselle = 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(in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 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 start = 1 4 caselle = 1 2 3 4 5 6 7 4 5 6 7 8 9 10 m = 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 ...

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

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 start = 5 1 caselle = 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 caselle = 1 5 m = 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 start = 4 1 caselle = 4 5 1 2 m = 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 7 9 0 0 0 start = 1 4 caselle = 1 2 4 5 m = 0 0 0 2 6 0 0 0 0 6 0 0 0 0 0 2 0 0 0 0 7 9 0 0 0 start = 3 1 caselle = 3 4 5 1 2 3 m = 0 0 0 2 6 0 0 0 0 6 9 0 0 0 0 2 10 0 0 0 7 9 1 0 0 start = 1 3 caselle = 1 2 3 3 4 5 m = 0 0 3 2 6 0 0 0 9 6 9 0 0 0 6 2 10 0 0 0 7 9 1 0 0 start = 2 1 caselle = 2 3 4 5 1 2 3 4 m = 0 0 3 2 6 6 0 0 9 6 9 5 0 0 6 2 10 5 0 0 7 9 1 8 0 start = 1 2 caselle = 1 2 3 4 2 3 4 5 m = 0 10 3 2 6 6 0 1 9 6 9 5 0 2 6 2 10 5 0 9 7 9 1 8 0 start = 1 1 caselle = 1 2 3 4 5 1 2 3 4 5 m = 1 10 3 2 6 6 5 1 9 6 9 5 1 2 6 2 10 5 7 9 7 9 1 8 8 start = 1 1 caselle = 1 2 3 4 5 1 2 3 4 5 m = 1 10 3 2 6 6 5 1 9 6 9 5 1 2 6 2 10 5 7 9 7 9 1 8 8 1 10 3 2 6 6 5 1 9 6 9 5 1 2 6 2 10 5 7 9 7 9 1 8 8

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

m = 0 0 0 0 0 0 start = 3 1 caselle = 3 1 m = 0 0 0 0 2 0 start = 1 2 caselle = 1 2 m = 0 3 0 0 2 0 start = 2 1 caselle = 2 3 1 2 m = 0 3 2 0 2 4 start = 1 1 caselle = 1 2 1 2 m = 3 3 2 2 2 4 3 3 2 2 2 4

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

m = 0×5 empty double matrix start = -4 1 caselle = -4 1

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