Cody

# Problem 972. Set a diagonal

Solution 154307

Submitted on 26 Oct 2012
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));

tf = 1 loc = 1 M = 7 d = 0 tf = 1 loc = 1 M = 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));

tf = 1 1 1 loc = 11 17 23 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

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

tf = 1 1 loc = 5 12 M = 0.8594 0.0287 0.4711 0.0967 0.5186 0.8253 0.8055 0.4899 0.0596 0.8181 0.9730 0.0835 0.5767 0.1679 0.6820 0.8175 0.6490 0.1332 0.1829 0.9787 0.0424 0.7224 0.8003 0.1734 0.2000 0.7127 0.0714 0.1499 0.4538 0.3909 0.8865 -0.2000 0.5216 0.6596 0.4324 0.8314

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

Error: Assertion failed.

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

Error: Assertion failed.