Cody

Problem 972. Set a diagonal

Solution 1163886

Submitted on 15 Apr 2017
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 = 1; v = 7; d = 0; M2 = 7; assert(isequal(setDiagonal(M,v,d),M2)); assert(isequal(setDiagonal(M,v),M2));

ans = (1,1) 7 d = 0 ans = (1,1) 7

2   Fail
M = magic(5); v = 1:3; d = 2; M2 = M; M2([11,17,23]) = v; assert(isequal(setDiagonal(M,v,d),M2));

Index exceeds matrix dimensions. Error in spdiags (line 102) a((len(k)+1):len(k+1),:) = [i i+d(k) B(i+(m>=n)*d(k),k)]; Error in setDiagonal (line 3) spdiags(v',d,M) Error in TestPoint2 (line 6) assert(isequal(setDiagonal(M,v,d),M2));

3   Pass
M = rand(6); v = [.2 -.2]; d = -4; M2 = M; M2([5,12]) = v; assert(isequal(setDiagonal(M,v,d),M2));

ans = (1,1) 0.4503 (2,1) 0.7381 (3,1) 0.3726 (4,1) 0.3987 (5,1) 0.2000 (6,1) 0.7526 (1,2) 0.7317 (2,2) 0.7276 (3,2) 0.7302 (4,2) 0.4575 (5,2) 0.7644 (6,2) -0.2000 (1,3) 0.5002 (2,3) 0.7006 (3,3) 0.8778 (4,3) 0.5463 (5,3) 0.1029 (6,3) 0.4166 (1,4) 0.3342 (2,4) 0.4635 (3,4) 0.5847 (4,4) 0.9058 (5,4) 0.4853 (6,4) 0.8262 (1,5) 0.5607 (2,5) 0.0501 (3,5) 0.6706 (4,5) 0.4052 (5,5) 0.4465 (6,5) 0.1684 (1,6) 0.7083 (2,6) 0.7049 (3,6) 0.6242 (4,6) 0.7359 (5,6) 0.6242 (6,6) 0.9389

4   Pass
M = zeros(99); v = ones(1,99); d = 0; M2 = eye(99); assert(isequal(setDiagonal(M,v),M2)); assert(isequal(setDiagonal(M,v,d),M2));

d = 0 ans = (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1 (6,6) 1 (7,7) 1 (8,8) 1 (9,9) 1 (10,10) 1 (11,11) 1 (12,12) 1 (13,13) 1 (14,14) 1 (15,15) 1 (16,16) 1 (17,17) 1 (18,18) 1 (19,19) 1 (20,20) 1 (21,21) 1 (22,22) 1 (23,23) 1 (24,24) 1 (25,25) 1 (26,26) 1 (27,27) 1 (28,28) 1 (29,29) 1 (30,30) 1 (31,31) 1 (32,32) 1 (33,33) 1 (34,34) 1 (35,35) 1 (36,36) 1 (37,37) 1 (38,38) 1 (39,39) 1 (40,40) 1 (41,41) 1 (42,42) 1 (43,43) 1 (44,44) 1 (45,45) 1 (46,46) 1 (47,47) 1 (48,48) 1 (49,49) 1 (50,50) 1 (51,51) 1 (52,52) 1 (53,53) 1 (54,54) 1 (55,55) 1 (56,56) 1 (57,57) 1 (58,58) 1 (59,59) 1 (60,60) 1 (61,61) 1 (62,62) 1 (63,63) 1 (64,64) 1 (65,65) 1 (66,66) 1 (67,67) 1 (68,68) 1 (69,69) 1 (70,70) 1 (71,71) 1 (72,72) 1 (73,73) 1 (74,74) 1 (75,75) 1 (76,76) 1 (77,77) 1 (78,78) 1 (79,79) 1 (80,80) 1 (81,81) 1 (82,82) 1 (83,83) 1 (84,84) 1 (85,85) 1 (86,86) 1 (87,87) 1 (88,88) 1 (89,89) 1 (90,90) 1 (91,91) 1 (92,92) 1 (93,93) 1 (94,94) 1 (95,95) 1 (96,96) 1 (97,97) 1 (98,98) 1 (99,99) 1 ans = (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1 (6,6) 1 (7,7) 1 (8,8) 1 (9,9) 1 (10,10) 1 (11,11) 1 (12,12) 1 (13,13) 1 (14,14) 1 (15,15) 1 (16,16) 1 (17,17) 1 (18,18) 1 (19,19) 1 (20,20) 1 (21,21) 1 (22,22) 1 (23,23) 1 (24,24) 1 (25,25) 1 (26,26) 1 (27,27) 1 (28,28) 1 (29,29) 1 (30,30) 1 (31,31) 1 (32,32) 1 (33,33) 1 (34,34) 1 (35,35) 1 (36,36) 1 (37,37) 1 (38,38) 1 (39,39) 1 (40,40) 1 (41,41) 1 (42,42) 1 (43,43) 1 (44,44) 1 (45,45) 1 (46,46) 1 (47,47) 1 (48,48) 1 (49,49) 1 (50,50) 1 (51,51) 1 (52,52) 1 (53,53) 1 (54,54) 1 (55,55) 1 (56,56) 1 (57,57) 1 (58,58) 1 (59,59) 1 (60,60) 1 (61,61) 1 (62,62) 1 (63,63) 1 (64,64) 1 (65,65) 1 (66,66) 1 (67,67) 1 (68,68) 1 (69,69) 1 (70,70) 1 (71,71) 1 (72,72) 1 (73,73) 1 (74,74) 1 (75,75) 1 (76,76) 1 (77,77) 1 (78,78) 1 (79,79) 1 (80,80) 1 (81,81) 1 (82,82) 1 (83,83) 1 (84,84) 1 (85,85) 1 (86,86) 1 (87,87) 1 (88,88) 1 (89,89) 1 (90,90) 1 (91,91) 1 (92,92) 1 (93,93) 1 (94,94) 1 (95,95) 1 (96,96) 1 (97,97) 1 (98,98) 1 (99,99) 1

5   Pass
M = zeros(3); v = -1:1; d = 0; M2 = diag(v); assert(isequal(setDiagonal(M,v),M2)); assert(isequal(setDiagonal(M,v,d),M2));

d = 0 ans = (1,1) -1 (3,3) 1 ans = (1,1) -1 (3,3) 1

Suggested Problems

More from this Author11

Community Treasure Hunt

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

Start Hunting!