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:
Can't find a bug in nested loops

Subject: Can't find a bug in nested loops

From: Charles Dillon

Date: 16 Jun, 2011 09:55:07

Message: 1 of 3

The following code is supposed to use two vectors of scalars, train1 and train2, which are sorted in increasing order, and find the last element of train1 which is less than the current element in train2, for each iteration of a for loop which runs through all elements of train2. then it should calculate cor2 using
cor2 = cor2 + exp(-(train2(i)-train1(place_in_1))/tau)*f1(place_in_1);
The f1 element in each case is defined, as are train_2_size (which is numel(train2)) and tau.

The problem is, the code keeps running the while loop indefinitely. Whenever I stop execution of the code, it says that it is on line 11 (the end of the while loop). What am I doing wrong?


cor2=0;
place_in_1=1;
for i=1:train_2_size
    while train1(place_in_1)<train2(i) && place_in_1<train_1_size
        if place_in_1<train_1_size && train1(place_in_1+1)<train2(i)
            place_in_1 = place_in_1+1;
        end
    end %line 11
    if train1(place_in_1) < train2(i)
        cor2 = cor2 + exp(-(train2(i)-train1(place_in_1))/tau)*f1(place_in_1);
    end
end

Any help is appreciated.

Subject: Can't find a bug in nested loops

From: Rune Allnor

Date: 16 Jun, 2011 10:18:05

Message: 2 of 3

On Jun 16, 11:55 am, "Charles Dillon" <charl...@live.ie> wrote:
> The following code is supposed to use two vectors of scalars, train1 and train2, which are sorted in increasing order, and find the last element of train1 which is less than the current element in train2, for each iteration of a for loop which runs through all elements of train2. then it should calculate  cor2 using
> cor2 = cor2 + exp(-(train2(i)-train1(place_in_1))/tau)*f1(place_in_1);
> The f1 element in each case is defined, as are train_2_size (which is numel(train2)) and tau.
>
> The problem is, the code keeps running the while loop indefinitely. Whenever I stop execution of the code, it says that it is on line 11 (the end of the while loop). What am I doing wrong?

>     while train1(place_in_1)<train2(i) && place_in_1<train_1_size
>         if place_in_1<train_1_size && train1(place_in_1+1)<train2(i)
>             place_in_1 = place_in_1+1;
>         end
>     end          %line 11

The while loop depends on place_in_1 being updated between
iterations, but the inner IF conditional prevents that update
from happening in some cases.

What you need is a rule for how to update place_in_1 in
the case when the IF-conditional at line 8 does *not* hold.

Rune

Subject: Can't find a bug in nested loops

From: Charles Dillon

Date: 16 Jun, 2011 13:49:05

Message: 3 of 3

Rune Allnor <allnor@tele.ntnu.no> wrote in message <22fadb37-abc0-4180-beb4-20a074c59f77@hd10g2000vbb.googlegroups.com>...
> On Jun 16, 11:55 am, "Charles Dillon" <charl...@live.ie> wrote:
> > The following code is supposed to use two vectors of scalars, train1 and train2, which are sorted in increasing order, and find the last element of train1 which is less than the current element in train2, for each iteration of a for loop which runs through all elements of train2. then it should calculate  cor2 using
> > cor2 = cor2 + exp(-(train2(i)-train1(place_in_1))/tau)*f1(place_in_1);
> > The f1 element in each case is defined, as are train_2_size (which is numel(train2)) and tau.
> >
> > The problem is, the code keeps running the while loop indefinitely. Whenever I stop execution of the code, it says that it is on line 11 (the end of the while loop). What am I doing wrong?
>
> >     while train1(place_in_1)<train2(i) && place_in_1<train_1_size
> >         if place_in_1<train_1_size && train1(place_in_1+1)<train2(i)
> >             place_in_1 = place_in_1+1;
> >         end
> >     end          %line 11
>
> The while loop depends on place_in_1 being updated between
> iterations, but the inner IF conditional prevents that update
> from happening in some cases.
>
> What you need is a rule for how to update place_in_1 in
> the case when the IF-conditional at line 8 does *not* hold.
>
> Rune
Thanks very much, that fixed it!

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