Cody

# Problem 972. Set a diagonal

Solution 2165726

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

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

Matrix dimensions must agree. Error in setDiagonal (line 8) a+(a==0).*M; Error in Test2 (line 6) assert(isequal(setDiagonal(M,v,d),M2));

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

Matrix dimensions must agree. Error in setDiagonal (line 8) a+(a==0).*M; Error in Test3 (line 6) assert(isequal(setDiagonal(M,v,d),M2));

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

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

### Community Treasure Hunt

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

Start Hunting!