Matlab计数器将计数器停在一定值

时间:2013-02-21 21:40:53

标签: matlab counter

我正在使用Matlab,我已经实现了一个阈值函数来勾选计数器以完成我需要它做的事情。 我想知道是否有办法可以让计数器一旦达到某个值(最大值或最小值)就停止计数也就是说...我的计数器已达到40,现在我希望不再计算门槛值。

2 个答案:

答案 0 :(得分:0)

可能是这样的:

counter = 0;    
for i = 1:100
    if(condition)
        doSomething();
        counter = counter + 1;
    end
    if(counter == 40)
         break;
    end
end

counter = 0;    
for i = 1:100
    if(condition)
        doSomething();
        if(counter < 40)
            counter = counter + 1;
        end
    end
end

取决于你的意思。

答案 1 :(得分:0)

另一个(更简单的)选项是使用while循环:

counter=0;
    while counter<=40
         if condition==true
             counter=counter+1
             DoSomething ()
         end
    end