Cody

# Problem 972. Set a diagonal

Solution 1210672

Submitted on 12 Jun 2017 by LY Cao
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));

[Warning: File: setDiagonal.m Line: 2 Column: 7 This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB. See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details.] ans = 0 ans = (1,1) 7 ans = 0 ans = (1,1) 7

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

ans = 2 ans = (1,1) 17 (2,1) 23 (3,1) 4 (4,1) 10 (5,1) 11 (1,2) 24 (2,2) 5 (3,2) 6 (4,2) 12 (5,2) 18 (1,3) 1 (2,3) 7 (3,3) 13 (4,3) 19 (5,3) 25 (1,4) 8 (2,4) 2 (3,4) 20 (4,4) 21 (5,4) 2 (1,5) 15 (2,5) 16 (3,5) 3 (4,5) 3 (5,5) 9

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

ans = -4 ans = (1,1) 0.0486 (2,1) 0.0567 (3,1) 0.3694 (4,1) 0.1702 (5,1) 0.2000 (6,1) 0.5365 (1,2) 0.6750 (2,2) 0.9059 (3,2) 0.8916 (4,2) 0.9112 (5,2) 0.2619 (6,2) -0.2000 (1,3) 0.1398 (2,3) 0.9397 (3,3) 0.3005 (4,3) 0.1871 (5,3) 0.8336 (6,3) 0.1397 (1,4) 0.5148 (2,4) 0.2484 (3,4) 0.3996 (4,4) 0.9202 (5,4) 0.9564 (6,4) 0.3202 (1,5) 0.7483 (2,5) 0.3004 (3,5) 0.9843 (4,5) 0.8812 (5,5) 0.2061 (6,5) 0.0795 (1,6) 0.9470 (2,6) 0.5650 (3,6) 0.8036 (4,6) 0.6758 (5,6) 0.9092 (6,6) 0.5749

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));

ans = 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 = 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

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));

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