multiple conditions in a for loop matlab

12,391

Solution 1

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)

Solution 2

To plot the line y = x:

x = 1:100;
y = 1:100;

plot(x, y);

You don't need a loop at all if that's all you're trying to do.

That said, to answer your original question you cannot have multiple conditions in a for loop, for that you want a nested for loop as @DennisJaheruddin has shown.

Solution 3

% To plot y=x, you can simply use:

x=1:100;
y=x;
plot(x,y)

However, if you want to put multiple condition in a for loop, use:

for x=1:100
    for y=1:100
        plot(x,y);
        continue
    end
end
Share:
12,391
Chris 'Namikaze Kurisu' Fraser
Author by

Chris 'Namikaze Kurisu' Fraser

Updated on June 04, 2022

Comments

  • Chris 'Namikaze Kurisu' Fraser
    Chris 'Namikaze Kurisu' Fraser almost 2 years

    I am trying to write a for loop with multiple conditions, for example:

    for i=1:100 && j=1:100
        plot(i,j)
    end
    

    could you guys help me out please, this is my first time doing this