From: "Steve Amphlett" <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: finding indentical neighbor elements
Date: Mon, 13 Apr 2009 19:01:02 +0000 (UTC)
Organization: Ricardo UK Ltd
Lines: 15
Message-ID: <gs025e$4el$>
References: <>
Reply-To: "Steve Amphlett" <>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1239649262 4565 (13 Apr 2009 19:01:02 GMT)
NNTP-Posting-Date: Mon, 13 Apr 2009 19:01:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 43398
Xref: comp.soft-sys.matlab:532396

Bruce Bowler <> wrote in message <>...
> Sorry for the not-so-concise subject, I couldn't come up with one in less 
> than 10 words...
> I have a vector that's some 400,000 elements long.  Periodically in this 
> vector there are segments where there are "n" elements in a row that are 
> identical.  Is there a "matlab-like" way to find where those segments 
> are?  Ideally, I'd like the index of the first element and the last 
> element ("n" is variable within the vector) but would settle (beggars 
> can't be choosers :-) for either first or last.  Also ideally, it should 
> handle NaN, +inf and -inf as well as finite numbers.

I did one like this a while back, but it's Easter hols, so no ML at home and a pile of "stuff" tomorrow.

The basic tool here is sparse.  Your vector can be cut into chunks with diff.  Whenever diff is non-zero, increment your column number.  Row numbers stay the same.  Now you have a sparse matrix with each column representing a "run" if you've done the diff's right and have understood spare's calling convention.  The rest is for you to finish.   You could end up with a very pleasing one-liner.