Please do this in Matlab
Using a single for-loop and no if-statements, write a code that
will create a 10x10 matrix
with 2 on the diagonal, and -1 on any entry right next to the
diagonal.
for i = 1:10
A(i, 1:10) = [zeros(1, i-1) 2 -1*ones(1, 10-i) ];
end
A
% Explanation
% zeros(1, i-1) gives array of i-1 zeros
% ones(1, 10-i) gives array of 10-i ones
% -1 is multiplied with above to get 10-i -1s
% Hit the thumbs up if you are fine with the answer. Happy Learning!
Get Answers For Free
Most questions answered within 1 hours.