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:
Break out of IF statement and return to FOR loop + store the row number where break happened

Subject: Break out of IF statement and return to FOR loop + store the row number where break happened

From: Saad

Date: 5 Nov, 2013 11:04:15

Message: 1 of 8

Dear all,

I would like to break out of IF statement and return to the FOR loop. However, I would like the For loop to start from the row number where the break happened (by the same occasion I would like to store the value of that row).

For example:

for i=1:100

if condition is true
do something and break (give me the row number of the break and the value)
else
do something else

end

end
Thank you very much for your help

Best

S

Subject: Break out of IF statement and return to FOR loop + store the row

From: Jomar Bueyes

Date: 5 Nov, 2013 12:09:31

Message: 2 of 8

On Tuesday, November 5, 2013 6:04:15 AM UTC-5, Saad wrote:
> Dear all,
>
>
>
> I would like to break out of IF statement and return to the FOR loop. However, I would like the For loop to start from the row number where the break happened (by the same occasion I would like to store the value of that row).
>
>
>
> For example:
>
>
>
> for i=1:100
>
>
>
> if condition is true
>
> do something and break (give me the row number of the break and the value)
>
> else
>
> do something else
>
>
>
> end
>
>
>
> end
>
> Thank you very much for your help
>
>
>
> Best
>
>
>
> S

Hi,

I'm assuming that the loop index is the row number. If that is the case, change the 'for' loop to a 'while' loop:

j = 1;
while j <= 100
    if condition is true
       do something
    else
       do something else
       j = j + 1; % Next row only if condition is false <-- this is the trick
    end % if condition
end % while loop

Hope this helps,

Jomar

Subject: Break out of IF statement and return to FOR loop + store the

From: dpb

Date: 5 Nov, 2013 22:36:43

Message: 3 of 8

On 11/5/2013 6:09 AM, Jomar Bueyes wrote:
> On Tuesday, November 5, 2013 6:04:15 AM UTC-5, Saad wrote:
>> Dear all,
>> I would like to break out of IF statement and return to the FOR
>> loop. However, I would like the For loop to start from the row
>> number where the break happened (by the same occasion I would like
>> to store the value of that row).
>>
>> For example:
>>
>> for i=1:100
>> if condition is true
>> do something and break (give me the row number of the break and
>> the value)
>> else
>> do something else
>> end
>> end
>
...

> I'm assuming that the loop index is the row number. If that is the
> case, change the 'for' loop to a 'while' loop:
>
> j = 1;
> while j<= 100
> if condition is true
> do something
> else
> do something else
> j = j + 1; % Next row only if condition is false<-- this is the trick
> end % if condition
> end % while loop

I'm not at all sure what OP is actually asking for wrt to "starting the
FOR loop" differently than what the loop is doing anyway, but note that
your above will simply loop ad nauseum when the condition is true as
written since nothing happens to the index there...

--

Subject: Break out of IF statement and return to FOR loop + store the row

From: Saad

Date: 6 Nov, 2013 12:43:06

Message: 4 of 8

Hi Jomar,

Thank you very much for your reply.

This is the code that you suggested and it helped me a lot. As you can see I changed it slightly to adapt it to my situation.

%%
 j = 1;
 while j <= 100
     if condition is true
        do something
         B(j) % store outputs
         j = j + 1;
     else
        do something else
        j ="some value";
       C(j) % store another output
     end % if condition
 end % while loop
%%

I am stuck in defining "some value" in the second part of the "if" statement (i.e. else). Basically, i would like to give "C(j)" the value of the first row of B(j). If the condition is true then "j" is being incremented by 1 but as soon as the condition is false, I would like C(j) to be equal to the first value of B(j). In other terms, C(j) won't be equal to any value if the condition is true but if condition is false C(j) should have all the first rows of B(j). Please note that by "first row" I mean the first value of B(j) that we get after the condition is false. For example, if the condition is false at j=50, 100, 200 then C(j) should contain the output at row 50,100 and 200.

Any help or guidance on this one would be much appreciated. Thanks a lot

S



Jomar Bueyes <jomarbueyes@hotmail.com> wrote in message <eb3bb20b-6880-49a2-88bf-dce38205d246@googlegroups.com>...
> On Tuesday, November 5, 2013 6:04:15 AM UTC-5, Saad wrote:
> > Dear all,
> >
> >
> >
> > I would like to break out of IF statement and return to the FOR loop. However, I would like the For loop to start from the row number where the break happened (by the same occasion I would like to store the value of that row).
> >
> >
> >
> > For example:
> >
> >
> >
> > for i=1:100
> >
> >
> >
> > if condition is true
> >
> > do something and break (give me the row number of the break and the value)
> >
> > else
> >
> > do something else
> >
> >
> >
> > end
> >
> >
> >
> > end
> >
> > Thank you very much for your help
> >
> >
> >
> > Best
> >
> >
> >
> > S
>
> Hi,
>
> I'm assuming that the loop index is the row number. If that is the case, change the 'for' loop to a 'while' loop:
>
> j = 1;
> while j <= 100
> if condition is true
> do something
> else
> do something else
> j = j + 1; % Next row only if condition is false <-- this is the trick
> end % if condition
> end % while loop
>
> Hope this helps,
>
> Jomar

Subject: Break out of IF statement and return to FOR loop + store the

From: dpb

Date: 6 Nov, 2013 14:23:12

Message: 5 of 8

On 11/6/2013 6:43 AM, Saad wrote:

...[top=posting repaired--don't do that, hard conversation follow makes]...

> Jomar Bueyes <jomarbueyes@hotmail.com> wrote in message
> <eb3bb20b-6880-49a2-88bf-dce38205d246@googlegroups.com>...
>> On Tuesday, November 5, 2013 6:04:15 AM UTC-5, Saad wrote:
...

>> I'm assuming that the loop index is the row number. If that is the
>> case, change the 'for' loop to a 'while' loop:
>>
>> j = 1;
>> while j <= 100
>> if condition is true
>> do something
>> else
>> do something else
>> j = j + 1; % Next row only if condition is false <-- this is the trick
>> end % if condition
>> end % while loop
...

 > This is the code that you suggested and it helped me a lot. As you
 > can see I changed it slightly to adapt it to my situation.
 >
 > j = 1;
 > while j <= 100
 > if condition is true
 > do something
 > B(j) % store outputs
 > j = j + 1;
 > else
 > do something else
 > j ="some value"; C(j) % store another output
 > end % if condition end % while loop
 >
 > I am stuck in defining "some value" in the second part of the "if"
 > statement (i.e. else). ... i would like to give "C(j)" the value
 > of the first row of B(j). If the condition is true then "j" is being
 > incremented by 1 but as soon as the condition is false, I would like
 > C(j) to be equal to the first value of B(j). In other terms, C(j)
 > won't be equal to any value if the condition is true but if condition
 > C(j) should have all the first rows of B(j). Please note that by
 > "first row" I mean the first value of B(j) that we get after the
 > condition is false. For example, if the condition is false at j=50,
 > 100, 200 then C(j) should contain the output at row 50,100 and 200.
...

Need a second index as well is all...

j = 1; k=0; % NB these alias the builtin Matlab i,j...
while j <= 100
   if condition is true
     do something
     B(j) = % store outputs
     j = j + 1;
   else
     do something else
     k=k+1;
     C(j) = B(i,:);
   end % if condition
end % while loop

Altho insufficient detail given to tell for sure if possible for the
actual condition, the "Matlab-y" way would be to vectorize this with
logical addressing.

idx=B(:,1)==condition; % logical vector only T where condition met
C=B(idx,:); % C=the array B for those locations

--

Subject: Break out of IF statement and return to FOR loop + store the

From: dpb

Date: 6 Nov, 2013 14:33:09

Message: 6 of 8

On 11/6/2013 8:23 AM, dpb wrote:
...

> Need a second index as well is all...
>
> j = 1; k=0; % NB these alias the builtin Matlab i,j...
> while j <= 100
> if condition is true
> do something
> B(j) = % store outputs
> j = j + 1;
> else
> do something else
> k=k+1;
> C(j) = B(i,:);
> end % if condition
> end % while loop
>
ERRATA:

      C(j) = B(i,:);

is

      C(k) = B(j,:);

of course. Also, it still will suffer as written w/ never getting past
that point because j ceases to be updated at that time. The increment
for j needs to be outside the if...else...end block.

j = 0; k=0; % NB these alias the builtin Matlab i,j...
while j <= 100
   j = j + 1;
   if condition is true
     do something
     B(j) = % store outputs
   else
     do something else
     k=k+1;
     C(k) = B(j,:);
   end % if condition
end % while loop

--

Subject: Break out of IF statement and return to FOR loop + store the

From: Saad

Date: 6 Nov, 2013 17:13:08

Message: 7 of 8

dpb <none@non.net> wrote in message <l5djuu$d2s$1@speranza.aioe.org>...
> On 11/6/2013 8:23 AM, dpb wrote:
> ...
>
> > Need a second index as well is all...
> >
> > j = 1; k=0; % NB these alias the builtin Matlab i,j...
> > while j <= 100
> > if condition is true
> > do something
> > B(j) = % store outputs
> > j = j + 1;
> > else
> > do something else
> > k=k+1;
> > C(j) = B(i,:);
> > end % if condition
> > end % while loop
> >
> ERRATA:
>
> C(j) = B(i,:);
>
> is
>
> C(k) = B(j,:);
>
> of course. Also, it still will suffer as written w/ never getting past
> that point because j ceases to be updated at that time. The increment
> for j needs to be outside the if...else...end block.
>
> j = 0; k=0; % NB these alias the builtin Matlab i,j...
> while j <= 100
> j = j + 1;
> if condition is true
> do something
> B(j) = % store outputs
> else
> do something else
> k=k+1;
> C(k) = B(j,:);
> end % if condition
> end % while loop
>
> --

Thank you very much for the help and I will try it out!

Subject: Break out of IF statement and return to FOR loop + store the

From: dpb

Date: 6 Nov, 2013 17:25:20

Message: 8 of 8

On 11/6/2013 11:13 AM, Saad wrote:
...

> Thank you very much for the help and I will try it out!

Look at the previous vectorized solution, too...it's at least moderately
likely you can eliminate the loop structure entirely.

--

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