From: jonathan on
hello,
I would like to make a for loop with 2 variables specified for each individual loop.
i think it should go something like this

for[ x=1 && y=2, x=3 && y=4]
i=x+y
end

I want to get output
i=3
i=7
, but this does not work
can someone tell me the right way
thank you
From: Sean on
"jonathan " <senator314159(a)hotmail.com> wrote in message <i4499t$ipg$1(a)fred.mathworks.com>...
> hello,
> I would like to make a for loop with 2 variables specified for each individual loop.
> i think it should go something like this
>
> for[ x=1 && y=2, x=3 && y=4]
> i=x+y
> end
>
> I want to get output
> i=3
> i=7
> , but this does not work
> can someone tell me the right way
> thank you

You need to learn how a for loop works and how to nest them. Here's a good start.
doc for

or for this type of use a vectorized approach:
x = 1:2:3
y = 2:2:4
ii = x+y
From: Andy on
"jonathan " <senator314159(a)hotmail.com> wrote in message <i4499t$ipg$1(a)fred.mathworks.com>...
> hello,
> I would like to make a for loop with 2 variables specified for each individual loop.
> i think it should go something like this
>
> for[ x=1 && y=2, x=3 && y=4]
> i=x+y
> end
>
> I want to get output
> i=3
> i=7
> , but this does not work
> can someone tell me the right way
> thank you

Well, in this specific case:

for x=[1 3]
y = x+1;
i = x+y;
end

But if this was just a small example and you didn't always know that y = x+1, you could do something like:

x = 1:100;
y = sin(x);
result = zeros(1,100); % preallocate
for ix=1:100 % loop over an INDEX, not the values you actually care about
result(ix) = x(ix) + y(ix);
end
From: jonathan on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i449qo$m65$1(a)fred.mathworks.com>...
> "jonathan " <senator314159(a)hotmail.com> wrote in message <i4499t$ipg$1(a)fred.mathworks.com>...
> > hello,
> > I would like to make a for loop with 2 variables specified for each individual loop.
> > i think it should go something like this
> >
> > for[ x=1 && y=2, x=3 && y=4]
> > i=x+y
> > end
> >
> > I want to get output
> > i=3
> > i=7


> > , but this does not work
> > can someone tell me the right way
> > thank you
>
> Well, in this specific case:
>
> for x=[1 3]
> y = x+1;
> i = x+y;
> end
>
> But if this was just a small example and you didn't always know that y = x+1, you could do something like:
>
> x = 1:100;
> y = sin(x);
> result = zeros(1,100); % preallocate
> for ix=1:100 % loop over an INDEX, not the values you actually care about
> result(ix) = x(ix) + y(ix);
> end


I can tell you guys know what your doing, but i think maybe i did not explain it right. (either that or i just totally dont get your answer and im sorry)
I was just giving a small example so I could apply it to a more complex example.

Each time i have the for loop run i want to set x and y to have different numbers
for the very first loop i might want x =5 and y=10

(that was just one loop), then for the second loop i might want x=7 and y=15

so in all there was only 2 loops in this example , but 4 different variables (2 different variables in each loop)
can someone give me an example like that, i would be most thankfull
From: Sean on
"jonathan " <senator314159(a)hotmail.com> wrote in message <i44b7o$jo1$1(a)fred.mathworks.com>...
> "Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i449qo$m65$1(a)fred.mathworks.com>...
> > "jonathan " <senator314159(a)hotmail.com> wrote in message <i4499t$ipg$1(a)fred.mathworks.com>...
> > > hello,
> > > I would like to make a for loop with 2 variables specified for each individual loop.
> > > i think it should go something like this
> > >
> > > for[ x=1 && y=2, x=3 && y=4]
> > > i=x+y
> > > end
> > >
> > > I want to get output
> > > i=3
> > > i=7
>
>
> > > , but this does not work
> > > can someone tell me the right way
> > > thank you
> >
> > Well, in this specific case:
> >
> > for x=[1 3]
> > y = x+1;
> > i = x+y;
> > end
> >
> > But if this was just a small example and you didn't always know that y = x+1, you could do something like:
> >
> > x = 1:100;
> > y = sin(x);
> > result = zeros(1,100); % preallocate
> > for ix=1:100 % loop over an INDEX, not the values you actually care about
> > result(ix) = x(ix) + y(ix);
> > end
>
>
> I can tell you guys know what your doing, but i think maybe i did not explain it right. (either that or i just totally dont get your answer and im sorry)
> I was just giving a small example so I could apply it to a more complex example.
>
> Each time i have the for loop run i want to set x and y to have different numbers
> for the very first loop i might want x =5 and y=10
>
> (that was just one loop), then for the second loop i might want x=7 and y=15
>
> so in all there was only 2 loops in this example , but 4 different variables (2 different variables in each loop)
> can someone give me an example like that, i would be most thankfull


You're improperly using the word 'loop'. In your example you have one loop with two iterations. That depends on two different variables.

x = [5;7];
y = [10;15];
vals = zeros(size(y));
for ii = 1:2
vals(ii) = x(ii) - y(ii)
end

or you could just do what I did above. If you want a random number look at:
help rand

I would highly recommend reading the getting started documentation.