Path: news.mathworks.com!not-for-mail
From: "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com>
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$1@fred.mathworks.com>
References: <74hctpF13bmmpU1@mid.individual.net>
Reply-To: "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1239649262 4565 172.30.248.35 (13 Apr 2009 19:01:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 13 Apr 2009 19:01:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 43398
Xref: news.mathworks.com comp.soft-sys.matlab:532396

Bruce Bowler <bbowler@bigelow.org> wrote in message <74hctpF13bmmpU1@mid.individual.net>...
> 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.