# MATLAB: How to generate a matrix with number of lines inserted from keyboard

keyboard inputmatrix

I have to generate a matrix with i lines and j=i+1 collumns whose elements:
=2 if i=j;
=-1 if | i -j |=1;
=0 otherwise.
Here's my attempt at solving this problem:
``prompt='Insert number of lines';i=input(prompt)j=i+1% getting the number of lines and collumnsif i==j A=2*ones(i,j);end% checking the first condition for i and jelse if abs(i-j)==1A=-1*ones(i,j);end%  checking the second condition for i and jelseA=zeros(i,j)% checking the third condition for i and jendendA% displaying matrix A``
And I get this error when I try run it:
``else if abs(i-j)==1 |Error: Illegal use of reserved keyword "else".``
I don't understand what I'm doing wrong.

• You had too many end statements. They were defining very short if blocks, and so some of those blocks did not begin correctly as a result.
Your code still has problems (it is not doing what you want it to). Since this appears to be a homework assignment, I will defer to you to get it running correctly.
This runs:
``if i==j    A=2*ones(i,j);    % checking the first condition for i and jelseif abs(i-j)==1    A=-1*ones(i,j);    %  checking the second condition for i and jelse    A=zeros(i,j)    % checking the third condition for i and jend``