Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Identifying repeating pattern in a vector
Date: Tue, 3 Nov 2009 15:11:02 +0000 (UTC)
Organization: Erasmus MC
Lines: 38
Message-ID: <hcph66$48c$1@fred.mathworks.com>
References: <e09c467a-b65f-4636-bb80-67ba02bff5da@d10g2000yqh.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257261062 4364 172.30.248.37 (3 Nov 2009 15:11:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 15:11:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:582066


Eyal <efleminge@gmail.com> wrote in message <e09c467a-b65f-4636-bb80-67ba02bff5da@d10g2000yqh.googlegroups.com>...
> Hi
> 
> I have a function running a loop where a value is added to a vector at
> each iteration. So, for example, if the vector is x, it could look
> like:
> 
> Iteration 1: x=[1]
> Iteration 2: x=[1,3]
> Iteration 3: x=[1,3,2]
> Iteration 4: x=[1,3,2,6]
> Iteration 5: x=[1,3,2,6,2]
> Iteration 6: x=[1,3,2,6,2,6]
> Iteration 7: x=[1,3,2,6,2,6,2]
> 
> and so on.
> 
> As can be seen in the example above, at some point a repeating pattern
> of values may appear in x; this indicates the function has entered an
> infinite loop. I would like to be able to catch the pattern, say for
> example after 3 repeats. In the example above, that would be when the
> last 6 values of x are [2,6,2,6,2,6]; however, I do not know the
> values in advance, nor do I know the length of the repeating pattern
> (e.g. it could be [2,4,6,3,2,4,6,3,2,4,6,3]), though for practical
> purposes three values should be sufficient to catch this error.
> 
> Can anyone suggest a way to identify this repeating pattern?
> 
> Thx

Why not compare the last N elements with the second-last N (if that is the right term for it?) elements? Something like

if isequal(X(end-N+1:end),X(end-N-N+1:end-N))
   error('we are repeating ourselves ...') ;
end
   
hth
Jos