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!