필터 지우기
필터 지우기

How can I fix this infinite loop?

조회 수: 2 (최근 30일)
Gavin Thompson
Gavin Thompson 2021년 10월 8일
댓글: Gavin Thompson 2021년 10월 8일
C = input('Enter a vector of coefficients for an odd-order polynomial: ');
valC = length(C);
count = 1;
while mod(valC,2) == 1
C = input('Try Again. Enter a vector of coefficients for an odd-order polynomial: ');
count = count + 1;
if count > 4
warning('Odd number of coefficients entered. Last element of user input removed.');
C(:,end) = [];
end
end
When the count variable reaches 5 I just want to produce the warning and remove the last number from the C array but when I run it, MATLAB produces the warning and removes the variable however it just starts the loop over again instead of ending after the last number in C array is removed.

채택된 답변

Walter Roberson
Walter Roberson 2021년 10월 8일
C = input('Enter a vector of coefficients for an odd-order polynomial: ');
count = 1;
while mod(numel(C),2) == 1
C = input('Try Again. Enter a vector of coefficients for an odd-order polynomial: ');
count = count + 1;
if count > 4
warning('Odd number of coefficients entered. Last element of user input removed.');
C(end) = [];
break;
end
end
Question: what do you want to do if the user enters the empty array -- such as if they just press return.
  댓글 수: 1
Gavin Thompson
Gavin Thompson 2021년 10월 8일
Good question, I don't think it'll be necessary to code that in right now but I think I know of a few ways I could code that possibility into my program if needed. Thank you for your insight!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by