我正在尝试编写具有多个条件的 for 循环,例如:
for i=1:100 && j=1:100
plot(i,j)
end
大家能帮帮我吗,这是我第一次这样做
我正在尝试编写具有多个条件的 for 循环,例如:
for i=1:100 && j=1:100
plot(i,j)
end
大家能帮帮我吗,这是我第一次这样做
As ogzd mentioned, this is how you can plot all combinations of i
and j
with a nested loop.
If you are specifically interested in plotting though, you probably don't need a double loop for that. Check out:
hold on
for i = 1:100
plot(i,1:100,'o')
end
Or even more vectorized:
[a b] = meshgrid(1:100,1:100)
plot(a,b,'o')
EDIT: maybe you are just looking for this:
x = 1:100;
plot(x,x) % As y = x , otherwise of course plot(x,y)
要绘制线 y = x:
x = 1:100;
y = 1:100;
plot(x, y);
如果这就是你想要做的,你根本不需要循环。
也就是说,要回答您的原始问题,您不能在 for 循环中拥有多个条件,因为您想要一个嵌套的 for 循环,如@DennisJaheruddin 所示。
% 要绘制 y=x,您可以简单地使用:
x=1:100;
y=x;
plot(x,y)
但是,如果要将多个条件放在 for 循环中,请使用:
for x=1:100
for y=1:100
plot(x,y);
continue
end
end
为了绘制 线条y = x
,您可以简单地做
x = 1:100;
plot( x, x, '.-' );
使用嵌套循环
尝试这个:
hold on
for i=1:100
for j=1:100
plot(i,j)
end
end