フィルターのクリア

breaking an if loop

76 ビュー (過去 30 日間)
Fred
Fred 2013 年 10 月 2 日
コメント済み: Fred 2013 年 10 月 2 日
In the following script, why doesn't the break in line 24 get executed?
clf
xx = 1:.01:2;
vec = 1:.01:2;
for i=1:101;
yy(i)= -1*vec(i)+2 ;
end;
plot(xx,yy,'r')
plot(xx,yy);
axis ([0 10 0 2]);
hold on
N = 100;
xo = 1.8; yo = 1.2;
dx = .01; dy = .01;
for i = 1:N
x(i) = xo - i*dx;
y(i) = yo - i*dy;
plot(x(i),y(i))
for j = 1:N
if y(i) < yy(j)&& x(i) < xx(j)
plot (x(i),y(i),'o')
break
end
end
end

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 2 日
t = false;
for i = 1:N
if t, break; end
x(i) = xo - i*dx;
y(i) = yo - i*dy;
plot(x(i),y(i))
for j = 1:N
if y(i) < yy(j)&& x(i) < xx(j)
t = true;
plot (x(i),y(i),'o')
break
end
end
end

その他の回答 (2 件)

Roger Stafford
Roger Stafford 2013 年 10 月 2 日
Are you sure the 'break' is not executed? You have placed it within the inner for-loop, so when it performs the break it continues to execute the outer loop. Read Mathworks' documentation: "In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop." In your case whenever you have an (i,j) pair for which i+j>81 and i-j>19 both become true, then the break should occur with no further j values for that i value. However it goes right on with the next value of i in the outer loop and this break should therefore occur repeatedly for a number of different values of i, once on each i.

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日
What made you think that this line is not executed?
  1 件のコメント
Fred
Fred 2013 年 10 月 2 日
Thanks,
It is not executing as I can observe from the results, Also I can remove it with no change in the results.

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

カテゴリ

Help Center および File ExchangeFormatting and Annotation についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by