Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 144950

Submitted on 5 Oct 2012

Incorrect

31Size
Leading solution size is 21.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
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 Release Notes for MATLAB Version 7.4, "Warning Generated by try-catch" for details.]
[> In verifyCode>evaluateCode at 227
  In verifyCode at 40
  In fevalJSON at 14]
2
Pass
 
%%
M = magic(5);
v = 1:3;
d = 2;
M2 = M;
M2([11,17,23]) = v;
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));
Error: Assertion failed.
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));