how to perform jump condtion or alternative solution for calling

조회 수: 3 (최근 30일)
singh
singh 2015년 4월 19일
댓글: Guillaume 2015년 4월 20일
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

답변 (1개)

pfb
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
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
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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by