How to go back to the input in a if?

3 vues (au cours des 30 derniers jours)
Luigi Bucci
Luigi Bucci le 20 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

Réponse acceptée

Luigi Bucci
Luigi Bucci le 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.

Plus de réponses (1)

DGM
DGM le 20 Jan 2022
Modifié(e) : DGM le 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 commentaires
Luigi Bucci
Luigi Bucci le 20 Jan 2022
Thanks for the help, I'm testing it. About the messages, they are required by my IT Teacher, cause, if the messages are missing, he doesn't understand the script, so he will valutate the work done with a low score, that means a low mark for the exam.
Luigi Bucci
Luigi Bucci le 20 Jan 2022
Modifié(e) : Luigi Bucci le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by