Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
for loop with 2 variables

Subject: for loop with 2 variables

From: jonathan

Date: 13 Aug, 2010 20:18:05

Message: 1 of 5

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

Subject: for loop with 2 variables

From: Andy

Date: 13 Aug, 2010 20:27:05

Message: 2 of 5

"jonathan " <senator314159@hotmail.com> wrote in message <i4499t$ipg$1@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

Subject: for loop with 2 variables

From: Sean

Date: 13 Aug, 2010 20:27:05

Message: 3 of 5

"jonathan " <senator314159@hotmail.com> wrote in message <i4499t$ipg$1@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

Subject: for loop with 2 variables

From: jonathan

Date: 13 Aug, 2010 20:51:04

Message: 4 of 5

"Andy " <myfakeemailaddress@gmail.com> wrote in message <i449qo$m65$1@fred.mathworks.com>...
> "jonathan " <senator314159@hotmail.com> wrote in message <i4499t$ipg$1@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

Subject: for loop with 2 variables

From: Sean

Date: 13 Aug, 2010 21:23:04

Message: 5 of 5

"jonathan " <senator314159@hotmail.com> wrote in message <i44b7o$jo1$1@fred.mathworks.com>...
> "Andy " <myfakeemailaddress@gmail.com> wrote in message <i449qo$m65$1@fred.mathworks.com>...
> > "jonathan " <senator314159@hotmail.com> wrote in message <i4499t$ipg$1@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.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us