how to perform jump condtion or alternative solution for calling

3 vues (au cours des 30 derniers jours)
singh
singh le 19 Avr 2015
Commenté : Guillaume le 20 Avr 2015
Suppose I take input at command window through input command
If it is in array then it show that value which was i enter
or if it is not in array then it will jump to take input again..
N=input('enter the no: ');
N1=X(N,:) it will show the coordinate value of N in the array X

Réponses (1)

pfb
pfb le 19 Avr 2015
Modifié(e) : pfb le 19 Avr 2015
Not very clear.
I'm not sure I understand what you want. I'm guessing here.
You have an array X (what's its size?)
Then you ask the user for a number N, presumably an integer.
Then you look up the Nth line of X.
You want to ask for N again if the input value is out of bounds (larger than the number of rows in X). You might also want to make sure that N is an integer.
If this is the case then
L = size(X,1);
c = 1; % condition to remain in the loop
while(c)
N=input('enter the no: ');
c = ~((N>0) && (N<L+1) && (N==floor(N)));
if(c)
fprintf('no. should be an integer between 1 and %d\n\n',L);
end
end
N1=X(N,:)
  2 commentaires
pfb
pfb le 20 Avr 2015
Please explain better.
In your example the input number (N) is used as a row index of the matrix X. My code checks whether N is an integer between 1 and the number of rows in X.
What is exactly you want? You say N should be "found in the array". But found where??? Should N appear in the Nth row of X??
Guillaume
Guillaume le 20 Avr 2015
No idea about what singh is asking, but another option to pfb's code is to actually attempt the indexing and if it fails prompt again:
while true %loop until we break out of it
N=input('enter the no: ');
try
N1 = X(N, :); %try indexing
break; %if indexing fails, this line will never be executed
end %loop around if indexing fails
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by