Cody

Problem 972. Set a diagonal

Solution 2732130

Submitted on 23 Jul 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));

ans = 7

Not enough input arguments. Error in setDiagonal (line 2) M - diag(diag(M,d),d) + diag(v,d) 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));

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

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

ans = 0.5239 0.4117 0.1138 0.1026 0.0506 0.8241 0.7128 0.2625 0.4658 0.0742 0.6830 0.6104 0.5814 0.1836 0.7182 0.0619 0.0402 0.1819 0.9701 0.9317 0.5425 0.7396 0.8552 0.1360 0.2000 0.1643 0.2392 0.4157 0.1126 0.2772 0.4516 -0.2000 0.2291 0.0285 0.5979 0.4391

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

Not enough input arguments. Error in setDiagonal (line 2) M - diag(diag(M,d),d) + diag(v,d) 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));

Not enough input arguments. Error in setDiagonal (line 2) M - diag(diag(M,d),d) + diag(v,d) Error in Test5 (line 5) assert(isequal(setDiagonal(M,v),M2));