How to go back to the input in a if?

2 次查看(过去 30 天)
%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

采纳的回答

Luigi Bucci
Luigi Bucci 2022-1-24
%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.

更多回答(1 个)

DGM
DGM 2022-1-20
编辑:DGM 2022-1-20
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 个评论
Luigi Bucci
Luigi Bucci 2022-1-20
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 2022-1-20
编辑:Luigi Bucci 2022-1-20
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by