How to go back to the input in a if?

1 view (last 30 days)
Luigi Bucci
Luigi Bucci on 20 Jan 2022
Answered: Luigi Bucci on 24 Jan 2022
%M=Righe della matrice A
m=4;
%N=Colonne della matrice A
input('n')
for i=1:4
for j=1:n
if (n<=13)&&(n>=3)
disp (['Il numero delle colonne scelto rispecchia le condizioni' ...
'imposte'])
else
disp(['Il numero di colonne da voi scelto non rispecchia le condizioni' ...
'imposte']);
break
end
end
end
A(i,j)=input('elementi-');
disp(A)
This is a code about the creation of a Matrix. I'm practising on MatLAB for an IT Exam at University. In this section I'm choosing the number of coloumns, that should be between 3 and 13. I don't know why it stucks on if and how I call again the input after the else. Can anyone help me?
Thanks for the answer.
Regards,
Luigi

Accepted Answer

Luigi Bucci
Luigi Bucci on 24 Jan 2022
%M=Righe della matrice A
M=4;
%N=Colonne della matrice A
N=input('n');
for i=1:4
for j=1:N
if (N<=13)&&(N>=3)
disp('Numero di colonne sufficienti, inizio formazione matrice');
A(i,j)=input('elementi-');
disp(A)
else
disp('Numero di colonne insufficienti, riavviare il programma');
return
end
end
end
My solution to the issue after writing this part again.

More Answers (1)

DGM
DGM on 20 Jan 2022
Edited: DGM on 20 Jan 2022
This is a start
%M=Righe della matrice A
m = 4;
%N=Colonne della matrice A
nrange = [3 13]; % pull parameters out for flexibility
while true
% use descriptive prompts with a trailing space to improve readability
n = input('Specify the number of columns (%d-%d): ',nrange(1),nrange(2));
if (n<=nrange(2)) && (n>=nrange(1))
% Unless an affirmative message is required for the assignment, I would consider it redundant
%disp (['Il numero delle colonne scelto rispecchia le condizioni' ...
% 'imposte'])
break;
else
disp(['Il numero di colonne da voi scelto non rispecchia le condizioni' ...
'imposte']);
end
end
% enter elements columnwise
for j = 1:n
for i = 1:m
A(i,j) = input(sprintf('Enter A(%d,%d): ',i,j));
end
end
disp(A)
  2 Comments
Luigi Bucci
Luigi Bucci on 20 Jan 2022
I solved after re-writing it, this is the code:
%M=Righe della matrice A
M=4;
%N=Colonne della matrice A
N=input('n');
for i=1:4
for j=1:N
if (N<=13)&&(N>=3)
disp('Numero di colonne sufficienti, inizio formazione matrice');
A(i,j)=input('elementi-');
disp(A)
else
disp('Numero di colonne insufficienti, riavviare il programma');
return
end
end
end
Now it works perfectly and solves all the doubts.
I add it as a useful hint for someone else. Thanks for your help.
Good evening. Regards,
Luigi

Sign in to comment.

Tags

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by