필터 지우기
필터 지우기

Am beginner here, can anyone tell me What is wrong with the code with while loop?

조회 수: 1 (최근 30일)
The following code is a simplified version of my problem. Can anyone pointout why the loop is not terminating??
x=2;
y=2;
a=[x,y];
b=[10,10];
tf=isequal(a,b);
while tf==0
x=x+1;
y=y+1;
tf=isequal(a,b);
end
disp(a);

채택된 답변

James Tursa
James Tursa 2018년 5월 31일
편집: James Tursa 2018년 5월 31일
You never update the "a" vector inside the loop. It was built from the original values of x and y and never changes. To fix this:
while tf==0
x=x+1;
y=y+1;
a=[x,y]; % <-- add this line
tf=isequal(a,b);
end
  댓글 수: 3
Stephen23
Stephen23 2018년 5월 31일
"But the same code is not working with decimal increments"
Because the accumulated floating point error means that those values are NOT the same. See the answers to your later question:

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

추가 답변 (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