how to perform jump condtion or alternative solution for calling
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
pfb
on 19 Apr 2015
Edited: pfb
on 19 Apr 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 Comments
pfb
on 20 Apr 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
on 20 Apr 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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!