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:
while loop inside a for loop

Subject: while loop inside a for loop

From: Emma Robertson

Date: 1 Mar, 2012 12:32:14

Message: 1 of 6

Hi, can someone please tell me why "time" (t) is not updated correctly within the while loop? Please run this small code in your machine and you'll know what I'm taking about.

What I want is: for t=1 run the "while loop" until iq<=(numberc-1) then t=2 run the "while loop" again until iq<=(numberc-1), and so on.

Basically, this code runs through all the nonzero elements of A and attempts to divide them individually into two, once a certain time has passed. Please feel free to ask for clarifications.
 
Many thanks in advance.

A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
         0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
         0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
         0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
         0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
         0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
         0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
         0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
         0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
         0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
      
gridx=20;gridy=10; tarea=16;
Td=2;idx=0;iq=1;tsigma=1;

maxc=floor((gridx*gridy)/tarea);
numberc=max(max(A));

clocks=zeros(25,25);
clocks(1:25,1)=1;

t=1;
for t=1:25 % MCS
while iq<=(numberc-1)
[rows,cols]=find(A==iq);
centroid = mean([rows cols]);
mxr=max(rows);
mnr=min(rows);
mxc=max(cols);
mnc=min(cols);
if (length(centroid)==2)
ct1=round(centroid(2));
ct2=floor(centroid(1));
end
% vertical distance
vds=mxr-mnr;

a=max(max(A));

%% if time since last division exceeds Td
t
if( (t-idx)>Td)
A(mnr:ct2,mnc:mxc)=1+numberc;
end

b=max(max(A));
c=b-a;
if c>0;
tsigma=t;
clocks(iq,t)=tsigma;

af=find((clocks(iq,:))>0);
bf=af(end);
idx=clocks(iq,bf);
end

numberc=max(max(A));

iq=iq+1;

end
end

Subject: while loop inside a for loop

From: someone

Date: 1 Mar, 2012 15:32:12

Message: 2 of 6

"Emma Robertson" wrote in message <jinq8e$j1f$1@newscl01ah.mathworks.com>...
> Hi, can someone please tell me why "time" (t) is not updated correctly within the while loop? Please run this small code in your machine and you'll know what I'm taking about.
>
> What I want is: for t=1 run the "while loop" until iq<=(numberc-1) then t=2 run the "while loop" again until iq<=(numberc-1), and so on.
>
> Basically, this code runs through all the nonzero elements of A and attempts to divide them individually into two, once a certain time has passed. Please feel free to ask for clarifications.
>
> Many thanks in advance.
>
> A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
> 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
>
> gridx=20;gridy=10; tarea=16;
> Td=2;idx=0;iq=1;tsigma=1;
>
> maxc=floor((gridx*gridy)/tarea);
> numberc=max(max(A));
>
> clocks=zeros(25,25);
> clocks(1:25,1)=1;
>
> t=1;
> for t=1:25 % MCS
> while iq<=(numberc-1)
> [rows,cols]=find(A==iq);
> centroid = mean([rows cols]);
> mxr=max(rows);
> mnr=min(rows);
> mxc=max(cols);
> mnc=min(cols);
> if (length(centroid)==2)
> ct1=round(centroid(2));
> ct2=floor(centroid(1));
> end
> % vertical distance
> vds=mxr-mnr;
>
> a=max(max(A));
>
> %% if time since last division exceeds Td
> t
> if( (t-idx)>Td)
> A(mnr:ct2,mnc:mxc)=1+numberc;
> end
>
> b=max(max(A));
> c=b-a;
> if c>0;
> tsigma=t;
> clocks(iq,t)=tsigma;
>
> af=find((clocks(iq,:))>0);
> bf=af(end);
> idx=clocks(iq,bf);
> end
>
> numberc=max(max(A));
>
> iq=iq+1;
>
> end
> end

If you simply display the value of t between the for and while statements, you will see that t is incrementing from 1 to 25. So, what must be happening is that the first time through the for loop iq is less than or equal to (numberc-1) and thus the while loop executes and the value of t=1 is displayed each time thru the while loop. After that (for the other 24 times thru the for loop) iq is greater than (numberc-1) so whats inside the while loop never gets executed and thus t does not get displayed.

Perhaps you need to reset iq or numberc between the for and while loops. Or something similar.

Subject: while loop inside a for loop

From: Emma Robertson

Date: 1 Mar, 2012 16:37:17

Message: 3 of 6

"someone" wrote in message <jio4ps$ohn$1@newscl01ah.mathworks.com>...
> "Emma Robertson" wrote in message <jinq8e$j1f$1@newscl01ah.mathworks.com>...
> > Hi, can someone please tell me why "time" (t) is not updated correctly within the while loop? Please run this small code in your machine and you'll know what I'm taking about.
> >
> > What I want is: for t=1 run the "while loop" until iq<=(numberc-1) then t=2 run the "while loop" again until iq<=(numberc-1), and so on.
> >
> > Basically, this code runs through all the nonzero elements of A and attempts to divide them individually into two, once a certain time has passed. Please feel free to ask for clarifications.
> >
> > Many thanks in advance.
> >
> > A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
> > 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> > 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> > 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> > 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ...
> > 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> > 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> > 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> > 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ...
> > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
> >
> > gridx=20;gridy=10; tarea=16;
> > Td=2;idx=0;iq=1;tsigma=1;
> >
> > maxc=floor((gridx*gridy)/tarea);
> > numberc=max(max(A));
> >
> > clocks=zeros(25,25);
> > clocks(1:25,1)=1;
> >
> > t=1;
> > for t=1:25 % MCS
> > while iq<=(numberc-1)
> > [rows,cols]=find(A==iq);
> > centroid = mean([rows cols]);
> > mxr=max(rows);
> > mnr=min(rows);
> > mxc=max(cols);
> > mnc=min(cols);
> > if (length(centroid)==2)
> > ct1=round(centroid(2));
> > ct2=floor(centroid(1));
> > end
> > % vertical distance
> > vds=mxr-mnr;
> >
> > a=max(max(A));
> >
> > %% if time since last division exceeds Td
> > t
> > if( (t-idx)>Td)
> > A(mnr:ct2,mnc:mxc)=1+numberc;
> > end
> >
> > b=max(max(A));
> > c=b-a;
> > if c>0;
> > tsigma=t;
> > clocks(iq,t)=tsigma;
> >
> > af=find((clocks(iq,:))>0);
> > bf=af(end);
> > idx=clocks(iq,bf);
> > end
> >
> > numberc=max(max(A));
> >
> > iq=iq+1;
> >
> > end
> > end
>
> If you simply display the value of t between the for and while statements, you will see that t is incrementing from 1 to 25. So, what must be happening is that the first time through the for loop iq is less than or equal to (numberc-1) and thus the while loop executes and the value of t=1 is displayed each time thru the while loop. After that (for the other 24 times thru the for loop) iq is greater than (numberc-1) so whats inside the while loop never gets executed and thus t does not get displayed.
>
> Perhaps you need to reset iq or numberc between the for and while loops. Or something similar.

Thanks for your reply. I have tried this but for some reason it is not giving me what I want.
I'd like the code to run through all elements of A at each time step and examine the given condition.
Any one else can help please?

Subject: while loop inside a for loop

From: Roger Stafford

Date: 1 Mar, 2012 17:55:16

Message: 4 of 6

"Emma Robertson" wrote in message <jio8jt$99l$1@newscl01ah.mathworks.com>...
> Thanks for your reply. I have tried this but for some reason it is not giving me what I want.
> I'd like the code to run through all elements of A at each time step and examine the given condition.
> Any one else can help please?
- - - - - - -
  You should pay more attention to what "someone" has told you, Emma. I'll put it in different terms. After the while-loop exits in the first t = 1 run, it is because the iq<=(numberc-1) condition has become false. The variable 'iq' is not altered when the second run with t = 2 is begun, (the iq = iq + 1 step is inside the while-loop.) That means the iq<=(numberc-1) condition is still false and will continue to be false from then on. You must do something to iq after leaving the while-loop to allow the next while-loop to begin executing.

Roger Stafford

Subject: while loop inside a for loop

From: someone

Date: 1 Mar, 2012 20:33:20

Message: 5 of 6

"Roger Stafford" wrote in message <jiod64$os0$1@newscl01ah.mathworks.com>...
> "Emma Robertson" wrote in message <jio8jt$99l$1@newscl01ah.mathworks.com>...
> > Thanks for your reply. I have tried this

What exactly have you tried?

> > but for some reason it is not giving me what I want.
> > I'd like the code to run through all elements of A at each time step and examine the given condition.
> > Any one else can help please?
> - - - - - - -
> You should pay more attention to what "someone" has told you, Emma. I'll put it in different terms. After the while-loop exits in the first t = 1 run, it is because the iq<=(numberc-1) condition has become false. The variable 'iq' is not altered when the second run with t = 2 is begun, (the iq = iq + 1 step is inside the while-loop.) That means the iq<=(numberc-1) condition is still false and will continue to be false from then on. You must do something to iq after leaving the while-loop to allow the next while-loop to begin executing.
>
> Roger Stafford

Subject: while loop inside a for loop

From: Emma Robertson

Date: 1 Mar, 2012 21:15:21

Message: 6 of 6

"someone" wrote in message <jiomeg$s7m$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jiod64$os0$1@newscl01ah.mathworks.com>...
> > "Emma Robertson" wrote in message <jio8jt$99l$1@newscl01ah.mathworks.com>...
> > > Thanks for your reply. I have tried this
>
> What exactly have you tried?
>
> > > but for some reason it is not giving me what I want.
> > > I'd like the code to run through all elements of A at each time step and examine the given condition.
> > > Any one else can help please?
> > - - - - - - -
> > You should pay more attention to what "someone" has told you, Emma. I'll put it in different terms. After the while-loop exits in the first t = 1 run, it is because the iq<=(numberc-1) condition has become false. The variable 'iq' is not altered when the second run with t = 2 is begun, (the iq = iq + 1 step is inside the while-loop.) That means the iq<=(numberc-1) condition is still false and will continue to be false from then on. You must do something to iq after leaving the while-loop to allow the next while-loop to begin executing.
> >
> > Roger Stafford

Sorry for the confusion. I've put "iq=1;" just before the "while loop", and this answers my initial question. But, this directed me to another problem within the code that I'm trying to fix now. Thanks.

Tags for 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