how to perform jump condtion or alternative solution for calling
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
pfb
2015-4-19
编辑:pfb
2015-4-19
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 个评论
pfb
2015-4-20
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
2015-4-20
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!