Cody

# Problem 972. Set a diagonal

Solution 2800522

Submitted on 6 Aug 2020
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   Fail
M = 1; v = 7; d = 0; M2 = 7; assert(isequal(setDiagonal(M,v,d),M2)); assert(isequal(setDiagonal(M,v),M2));

index = 1 sz = 1 1 start = 1 M = 7 index = 2 index = 1 sz = 1 1

Not enough input arguments. Error in setDiagonal (line 4) if d > 0 Error in Test1 (line 6) assert(isequal(setDiagonal(M,v),M2));

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

index = 1 sz = 5 5 start = 3 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 index = 2 M = 17 24 1 8 15 23 5 7 2 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 index = 3 M = 17 24 1 8 15 23 5 7 2 16 4 6 13 20 3 10 12 19 21 3 11 18 25 2 9 index = 4

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

index = 1 sz = 6 6 start = 5 M = 0.8877 0.1575 0.9802 0.9041 0.1790 0.2821 0.1309 0.4081 0.2527 0.0596 0.6380 0.1135 0.5028 0.3811 0.7230 0.5744 0.6914 0.6100 0.3377 0.4429 0.3384 0.6367 0.4973 0.9742 0.2000 0.7730 0.4529 0.5915 0.6681 0.1941 0.0279 0.9642 0.7760 0.7663 0.6561 0.2157 index = 2 M = 0.8877 0.1575 0.9802 0.9041 0.1790 0.2821 0.1309 0.4081 0.2527 0.0596 0.6380 0.1135 0.5028 0.3811 0.7230 0.5744 0.6914 0.6100 0.3377 0.4429 0.3384 0.6367 0.4973 0.9742 0.2000 0.7730 0.4529 0.5915 0.6681 0.1941 0.0279 -0.2000 0.7760 0.7663 0.6561 0.2157 index = 3

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

index = 1 sz = 99 99

Not enough input arguments. Error in setDiagonal (line 4) if d > 0 Error in Test4 (line 5) assert(isequal(setDiagonal(M,v),M2));

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

index = 1 sz = 3 3

Not enough input arguments. Error in setDiagonal (line 4) if d > 0 Error in Test5 (line 5) assert(isequal(setDiagonal(M,v),M2));