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:
loop for with two control variables

Subject: loop for with two control variables

From: Borja

Date: 27 Oct, 2010 13:54:04

Message: 1 of 11

Hi,

I´m trying to do a loop for with two control variables, but i can´t get it. In another language, like C or Java, it´s possible, but i don´t know how to do this in Matlab.

For example, in C:

for(x=0,y=10;x+y<30;x++,y++)

Anybody can help me?

Greetins.

Thanks you.

Subject: loop for with two control variables

From: Sean

Date: 27 Oct, 2010 14:15:11

Message: 2 of 11


>
> for(x=0,y=10;x+y<30;x++,y++)
>

It really all depends on what you want to do:

%assuming order of operations (x+y)<30 is what you meant

%%%
[xx yy] = meshgrid(0:30,10:30); %create all combinations

idx = (xx+yy) < 30; %keep combinations that work

list = [xx(idx),yy(idx)]; %create a list to traverse with a single for-loop

for ii = 1:length(idx)
    x = list(ii,1);
    y = list(ii,2);
    %computations
end

%%%

However, the glory of MATLAB is that you probably don't even need the for-loop depending on what computations you're doing. I.e. you should be able to write the computations to just except two vectors (xx(idx),yy(idx)) or the full matrices xx,yy
and it'll treat them the way you want.

Subject: loop for with two control variables

From: Cris Luengo

Date: 27 Oct, 2010 14:47:04

Message: 3 of 11

"Borja " <borjash1983@gmail.com> wrote in message <ia9ats$19p$1@fred.mathworks.com>...
> Hi,
>
> I´m trying to do a loop for with two control variables, but i can´t get it. In another language, like C or Java, it´s possible, but i don´t know how to do this in Matlab.
>
> For example, in C:
>
> for(x=0,y=10;x+y<30;x++,y++)
>
> Anybody can help me?

I never understood loops with two control variables. In your case, for example, y is always == x+10, so why do you need the two variables?

for x=0:10
   y = x+10;
   ...
end

But you can probably just skip the looping altogether, as Sean said:

x = 0:10;
y = x+10;
do_something_with_x_and_y(x,y);

If you really want two independent variables, you can use a WHILE loop:

x = 0;
y = 0;
while (x+y)<30
   ...
   x = x+1;
   y = y+1;
end

Cheers,
Cris.

Subject: loop for with two control variables

From: Borja

Date: 28 Oct, 2010 06:36:07

Message: 4 of 11

Thanks for your responses, but i don´t need to know how to resolve the example in matlab, because it´s simply an example in order to show us that my problem could be solved using other programming languages.

There are some situations, very very complex, of course not always, in which the use of loop for with two control variables can be necessary, so, my question is: Is it possible to do that in matlab?

Thanks you.

Subject: loop for with two control variables

From: Cris Luengo

Date: 28 Oct, 2010 07:26:04

Message: 5 of 11

"Borja " <borjash1983@gmail.com> wrote in message <iab5kn$mpk$1@fred.mathworks.com>...
> Thanks for your responses, but i don´t need to know how to resolve the example in matlab, because it´s simply an example in order to show us that my problem could be solved using other programming languages.
>
> There are some situations, very very complex, of course not always, in which the use of loop for with two control variables can be necessary, so, my question is: Is it possible to do that in matlab?
>
> Thanks you.

Yes, it is. You didn't see the WHILE example I gave you?

Subject: loop for with two control variables

From: Borja Serrano

Date: 28 Oct, 2010 09:14:04

Message: 6 of 11

> Yes, it is. You didn't see the WHILE example I gave you?

Yes, Cris, I saw your WHILE example, but you`re using five lines (two for the initial values, two for increase the values and one for de WHILE), when a loop FOR with two control variables resumes it in one line.

if you have a program with thousand of lines and you need to use that many times, it´s better (one line * times) than (five lines * time), but i don´t discuss about the correct use of loop FOR with two control variables.
I just only wanted to know if that loop FOR exists or not in Matlab, no another way to do the same.

Thanks.

Subject: loop for with two control variables

From: Torsten

Date: 28 Oct, 2010 10:42:20

Message: 7 of 11

On Oct 28, 11:14 am, "Borja Serrano" <borjash1...@gmail.com> wrote:
> > Yes, it is. You didn't see the WHILE example I gave you?
>
> Yes, Cris, I saw your WHILE example, but you`re using five lines (two for the initial values, two for increase the values and one for de WHILE), when a loop FOR with two control variables resumes it in one line.
>
> if you have a program with thousand of lines and you need to use that many times, it´s better (one line * times) than (five lines * time), but i don´t discuss about the correct use of loop FOR with two control variables.
> I just only wanted to know if that loop FOR exists or not in Matlab, no another way to do the same.
>
> Thanks.

As the other posters already explained, there are ways to do this. But
the construct as such does not exist in Matlab. The reason is, that
the for in Matlab is in fact a foreach on the vector supllied after
'for'. So Matlab does not have an increment and also not a stopping
condition in the for statement:

for k = 1:2:10
...
end

is the same as

k = 1:2:10
for k
...
end

- Torsten

Subject: loop for with two control variables

From: Torsten

Date: 28 Oct, 2010 10:49:41

Message: 8 of 11

On Oct 28, 11:14 am, "Borja Serrano" <borjash1...@gmail.com> wrote:
> > Yes, it is. You didn't see the WHILE example I gave you?
>
> Yes, Cris, I saw your WHILE example, but you`re using five lines (two for the initial values, two for increase the values and one for de WHILE), when a loop FOR with two control variables resumes it in one line.
>
> if you have a program with thousand of lines and you need to use that many times, it´s better (one line * times) than (five lines * time), but i don´t discuss about the correct use of loop FOR with two control variables.
> I just only wanted to know if that loop FOR exists or not in Matlab, no another way to do the same.
>
> Thanks.

As the other posters already explained, there are ways to do this.
But
the construct as such does not exist in Matlab. The reason is, that
the for in Matlab is in fact a foreach on the vector supllied after
'for'. So Matlab does not have an increment and also not a stopping
condition in the for statement:

for k = 1:2:10
...
end


is the same as


v = 1:2:10;
for k = v
...
end


- Torsten

Subject: loop for with two control variables

From: Borja Serrano

Date: 28 Oct, 2010 11:09:04

Message: 9 of 11

Thanks you very much for your response Torsten. Now, i know the restriction of the loop FOR in Matlab.

Thanks again.

Subject: loop for with two control variables

From: Steven_Lord

Date: 28 Oct, 2010 14:11:22

Message: 10 of 11



"Torsten" <torsten.pietrek@topiks.de> wrote in message
news:9f69b574-d172-4133-9be7-db5b0302e729@v16g2000yqn.googlegroups.com...
> On Oct 28, 11:14 am, "Borja Serrano" <borjash1...@gmail.com> wrote:
>> > Yes, it is. You didn't see the WHILE example I gave you?
>>
>> Yes, Cris, I saw your WHILE example, but you`re using five lines (two for
>> the initial values, two for increase the values and one for de WHILE),
>> when a loop FOR with two control variables resumes it in one line.
>>
>> if you have a program with thousand of lines and you need to use that
>> many times, its better (one line * times) than (five lines * time), but
>> i dont discuss about the correct use of loop FOR with two control
>> variables.
>> I just only wanted to know if that loop FOR exists or not in Matlab, no
>> another way to do the same.
>>
>> Thanks.
>
> As the other posters already explained, there are ways to do this. But
> the construct as such does not exist in Matlab. The reason is, that
> the for in Matlab is in fact a foreach on the vector supllied after
> 'for'. So Matlab does not have an increment and also not a stopping
> condition in the for statement:
>
> for k = 1:2:10
> ...
> end
>
> is the same as
>
> k = 1:2:10
> for k
> ...
> end

You're basically correct, but you missed one subtlety that, if the OP wants
to loop over the two variables that will take on the same number of values,
gives a way to do what they want.

k = 1:2:10;
xy = [k; k.^2];
for m = xy
    x = m(1, 1);
    y = m(2, 1);
    fprintf('x = %d, y = %d\n', x, y); % "dummy" body
end

FOR iterates over the _columns_ of the loop expression. For the most common
case, where the loop expression is a row vector, this is the same as
iterating over the elements. If the loop expression is a matrix, though,
there's a distinct difference.

Note that this also isn't limited to numeric arrays.

c = cell(1, 5);
for k = 1:5
    c{k} = magic(k);
end
for m = c
    fprintf('This input is %d-by-%d.\n', size(m{1}));
end

And yes, this is documented behavior.

http://www.mathworks.com/help/techdoc/ref/for.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: loop for with two control variables

From: Torsten

Date: 28 Oct, 2010 19:46:04

Message: 11 of 11

"Steven_Lord" <slord@mathworks.com> wrote in message <iac0aa$7es$1@fred.mathworks.com>...
>
>
> "Torsten" <torsten.pietrek@topiks.de> wrote in message
> news:9f69b574-d172-4133-9be7-db5b0302e729@v16g2000yqn.googlegroups.com...
> > On Oct 28, 11:14 am, "Borja Serrano" <borjash1...@gmail.com> wrote:
> >> > Yes, it is. You didn't see the WHILE example I gave you?
> >>
> >> Yes, Cris, I saw your WHILE example, but you`re using five lines (two for
> >> the initial values, two for increase the values and one for de WHILE),
> >> when a loop FOR with two control variables resumes it in one line.
> >>
> >> if you have a program with thousand of lines and you need to use that
> >> many times, its better (one line * times) than (five lines * time), but
> >> i dont discuss about the correct use of loop FOR with two control
> >> variables.
> >> I just only wanted to know if that loop FOR exists or not in Matlab, no
> >> another way to do the same.
> >>
> >> Thanks.
> >
> > As the other posters already explained, there are ways to do this. But
> > the construct as such does not exist in Matlab. The reason is, that
> > the for in Matlab is in fact a foreach on the vector supllied after
> > 'for'. So Matlab does not have an increment and also not a stopping
> > condition in the for statement:
> >
> > for k = 1:2:10
> > ...
> > end
> >
> > is the same as
> >
> > k = 1:2:10
> > for k
> > ...
> > end
>
> You're basically correct, but you missed one subtlety that, if the OP wants
> to loop over the two variables that will take on the same number of values,
> gives a way to do what they want.
>
> k = 1:2:10;
> xy = [k; k.^2];
> for m = xy
> x = m(1, 1);
> y = m(2, 1);
> fprintf('x = %d, y = %d\n', x, y); % "dummy" body
> end
>
> FOR iterates over the _columns_ of the loop expression. For the most common
> case, where the loop expression is a row vector, this is the same as
> iterating over the elements. If the loop expression is a matrix, though,
> there's a distinct difference.
>
> Note that this also isn't limited to numeric arrays.
>
> c = cell(1, 5);
> for k = 1:5
> c{k} = magic(k);
> end
> for m = c
> fprintf('This input is %d-by-%d.\n', size(m{1}));
> end
>
> And yes, this is documented behavior.
>
> http://www.mathworks.com/help/techdoc/ref/for.html
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I am aware of the column looping feature, but I would not recommend to use it, because it is confusing for most readers of the code. I prefer explicit indices based on row vectors, because everybody will now what is going on when reading it.
- Torsten

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