%% Use if, elseif, and else for Conditional Assignment
% Create a matrix of 1s.

nrows = 4;
ncols = 6;
A = ones(nrows,ncols);

%
% Loop through the matrix and assign each element a new value. Assign 2 on the main diagonal, -1 on the adjacent diagonals, and 0 everywhere else.

for c = 1:ncols
    for r = 1:nrows
        
        if r == c
            A(r,c) = 2;
        elseif abs(r-c) == 1
            A(r,c) = -1;
        else
            A(r,c) = 0;
        end
        
    end
end
A

% Copyright 2015 The MathWorks, Inc.