Il am trying to change the elements of m to 0 when they reach a value superior to 10. The sequence is suppose to stop when all the elements equal 0 but it seem to act like an infinite loop when it should take less than 10 iterations to solve and I don't know why. What is the problem and how can I solve it?
clear;clc
m=[1,2,3,4,5];
l=length(m);
z=0;
while l~=z
m=m*1.5;
a=find(m>=10);
la=length(a);
if la>0
m(a)=0;
end
z=la;
end
a=m

 채택된 답변

Matt J
Matt J 2014년 3월 20일
편집: Matt J 2014년 3월 20일
Once you start replacing some of the m(i) with zeros, those m(i) remain zero and can never again reach a value of 10. Therefore z=sum(m>=10) can never reach l=length(m). You really want something like this,
while any(m)
m=m*1.5;
m(m>=10)=0;
end

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2014년 3월 20일

1 개 추천

Put a break point on the first line and then step through. You can out a breakpoint by clicking on the dash next to the while. Then click step to walk through each line investigating what each variable is at any point.

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 3월 20일

편집:

2014년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by