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 154314

Submitted on 26 Oct 2012

Incorrect

51Size
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));
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.9436    0.6718    0.8444    0.3868    0.7702    0.7218
    0.6377    0.6951    0.3445    0.9160    0.3225    0.4735
    0.9577    0.0680    0.7805    0.0012    0.7847    0.1527
    0.2407    0.2548    0.6753    0.4624    0.4714    0.3411
    0.2000    0.2240    0.0067    0.4243    0.0358    0.6074
    0.2891   -0.2000    0.6022    0.4609    0.1759    0.1917
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));
d =
     0
tf =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 32
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 33 through 48
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 49 through 64
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 65 through 80
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 81 through 96
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 97 through 99
     1     1     1
loc =
  Columns 1 through 8
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 9 through 16
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 17 through 24
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 25 through 32
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 33 through 40
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 41 through 48
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 49 through 56
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 57 through 64
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 65 through 72
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 73 through 80
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 81 through 88
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 89 through 96
        9801        9801        9801        9801        9801        9801        9801        9801
  Columns 97 through 99
        9801        9801        9801
M =
  Columns 1 through 16
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0   ...
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.