how do i direct a for loop, to end after a variable reaches a certain point say x>7200? x is being accumulated in the loop but it is just a value thats inside the loop. the loop is for i:1:7200 but x reaches 7200 first then i want it to stop

3 ビュー (過去 30 日間)
for i=1:n;
if (i == 1)
l=(.5+4.5*rand);
x(i,1)=l;
y(i,1)=mod(x(i),60);
else
l=(.5+4.5*rand);
x(i,1)=x(i-1,1)+l;
y(i,1)=mod(x(i),60);
end
if y(i,1)<=35
greencars(i,1)=1
else y(i,1)>35
inqueue(i,1)=1
end
end
  1 件のコメント
Matt Fig
Matt Fig 2012 年 12 月 1 日
George, next time you post some code, please use the {} Code button to format it so people can read it!

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2012 年 12 月 1 日
Insert this at the end of the loop, just before the final end statement:
if x(i, 1) > 7200
break; % break out of loop
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by