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 Question

Subject: Loop Question

From: Garrett

Date: 6 Feb, 2009 00:21:01

Message: 1 of 3

Hi,

I have a for loop that has an index from 1: length(y) where y is a previously defined Nx1 array. The problem I am having is the search that i run in the for loop finds data that contains multiple points on y at a time. I need to delete these points on y before the for loop cycles again to avoid duplicate data. Obviously this leads to a bit of a paradox because the higher index length(y) is getting shorter every cycle. I was wondering if anyone had an idea of how to stop the loop before it exceeds its index.

Thanks,

-Garrett

Subject: Loop Question

From: Roger Stafford

Date: 6 Feb, 2009 00:59:01

Message: 2 of 3

"Garrett " <bantong@onid.orst.edu> wrote in message <gmfvpd$26v$1@fred.mathworks.com>...
> Hi,
>
> I have a for loop that has an index from 1: length(y) where y is a previously defined Nx1 array. The problem I am having is the search that i run in the for loop finds data that contains multiple points on y at a time. I need to delete these points on y before the for loop cycles again to avoid duplicate data. Obviously this leads to a bit of a paradox because the higher index length(y) is getting shorter every cycle. I was wondering if anyone had an idea of how to stop the loop before it exceeds its index.
>
> Thanks,
>
> -Garrett

  It's hard to give you a valid suggestion without understanding the nature of your procedure. What you describe seems fraught with dangerous possibilities. Your for-loop generates an index which presumably refers to the y-values. What are you supposed to do when the index arrives at a place where the y has already been slated for deletion? Are you allowed to refer to it anyway? Also if you carry out the deletions in y as you go along, that breaks the connection between your index and the proper location in y. I would say you need to accumulate in a vector the indices of y's you want deleted but postpone the actual deletion until your looping is finished, at which time you can use this vector to delete them all at the same time.

  You still are faced with the problem of what to do with for-loop indices that are for y's that have already been scheduled for deletion. Perhaps you need a test for these so you can skip to the next iteration of the loop.

  You would have been well-advised to give more information about what is supposed to happen within your loop and the logic of how you decide which y's are to be deleted.

Roger Stafford

Subject: Loop Question

From: Husam Aldahiyat

Date: 6 Feb, 2009 06:45:03

Message: 3 of 3

"Garrett " <bantong@onid.orst.edu> wrote in message <gmfvpd$26v$1@fred.mathworks.com>...
> Hi,
>
> I have a for loop that has an index from 1: length(y) where y is a previously defined Nx1 array. The problem I am having is the search that i run in the for loop finds data that contains multiple points on y at a time. I need to delete these points on y before the for loop cycles again to avoid duplicate data. Obviously this leads to a bit of a paradox because the higher index length(y) is getting shorter every cycle. I was wondering if anyone had an idea of how to stop the loop before it exceeds its index.
>
> Thanks,
>
> -Garrett

Use a while loop.

c=1;
while c<length(y)
    CODE
    c=c+1;
end

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