http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945
MATLAB Central Newsreader  finding indentical neighbor elements
Feed for thread: finding indentical neighbor elements
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 13 Apr 2009 18:00:26 +0000
finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642340
Bruce Bowler
Sorry for the notsoconcise subject, I couldn't come up with one in less <br>
than 10 words...<br>
<br>
I have a vector that's some 400,000 elements long. Periodically in this <br>
vector there are segments where there are "n" elements in a row that are <br>
identical. Is there a "matlablike" way to find where those segments <br>
are? Ideally, I'd like the index of the first element and the last <br>
element ("n" is variable within the vector) but would settle (beggars <br>
can't be choosers :) for either first or last. Also ideally, it should <br>
handle NaN, +inf and inf as well as finite numbers.<br>
<br>
TIA,<br>
Bruce<br>
<br>
 <br>
+++<br>
Bruce Bowler  Experience is a good school, but the fees are high.<br>
1.207.633.9600   Heine <br>
bbowler@bigelow.org  <br>
+++

Mon, 13 Apr 2009 18:08:01 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642343
Marco
Bruce Bowler <bbowler@bigelow.org> wrote in message <74hctpF13bmmpU1@mid.individual.net>...<br>
> Sorry for the notsoconcise subject, I couldn't come up with one in less <br>
> than 10 words...<br>
> <br>
> I have a vector that's some 400,000 elements long. Periodically in this <br>
> vector there are segments where there are "n" elements in a row that are <br>
> identical. Is there a "matlablike" way to find where those segments <br>
> are? Ideally, I'd like the index of the first element and the last <br>
> element ("n" is variable within the vector) but would settle (beggars <br>
> can't be choosers :) for either first or last. Also ideally, it should <br>
> handle NaN, +inf and inf as well as finite numbers.<br>
> <br>
> TIA,<br>
> Bruce<br>
> <br>
>  <br>
> +++<br>
> Bruce Bowler  Experience is a good school, but the fees are high.<br>
> 1.207.633.9600   Heine <br>
> bbowler@bigelow.org  <br>
> +++<br>
<br>
Bruce, the "unique" command should do what you want.

Mon, 13 Apr 2009 18:25:26 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642349
Bruce Bowler
On Mon, 13 Apr 2009 18:08:01 +0000, Marco wrote:<br>
<br>
> Bruce Bowler <bbowler@bigelow.org> wrote in message<br>
> <74hctpF13bmmpU1@mid.individual.net>...<br>
>> Sorry for the notsoconcise subject, I couldn't come up with one in<br>
>> less than 10 words...<br>
>> <br>
>> I have a vector that's some 400,000 elements long. Periodically in<br>
>> this vector there are segments where there are "n" elements in a row<br>
>> that are identical. Is there a "matlablike" way to find where those<br>
>> segments are? Ideally, I'd like the index of the first element and the<br>
>> last element ("n" is variable within the vector) but would settle<br>
>> (beggars can't be choosers :) for either first or last. Also ideally,<br>
>> it should handle NaN, +inf and inf as well as finite numbers.<br>
>> <br>
>> TIA,<br>
>> Bruce<br>
>> <br>
> <br>
> Bruce, the "unique" command should do what you want.<br>
<br>
Maybe I'm missing something, but I don't see how unique (or any of it's <br>
outputs) tells me there are 3 2s in a row in the following case<br>
<br>
>> a=[8 2 2 2 7 4 4 8 9 1 2 3];<br>
>> [b,i,j]=unique(a);<br>
>> b<br>
b =<br>
1 2 3 4 7 8 9<br>
>> i<br>
i =<br>
10 11 12 7 5 8 9<br>
>> j<br>
j =<br>
6 2 2 2 5 4 4 6 7 1 2 3<br>
<br>
<br>
<br>
 <br>
+++<br>
Bruce Bowler  We learn from experience that men never learn<br>
1.207.633.9600  anything from experience.  Bernard Shaw <br>
bbowler@bigelow.org  <br>
+++

Mon, 13 Apr 2009 18:29:06 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642351
ImageAnalyst
On Apr 13, 2:00=A0pm, Bruce Bowler <bbow...@bigelow.org> wrote:<br>
> Sorry for the notsoconcise subject, I couldn't come up with one in less<br>
> than 10 words...<br>
><br>
> I have a vector that's some 400,000 elements long. =A0Periodically in thi=<br>
s<br>
> vector there are segments where there are "n" elements in a row that are<br>
> identical. =A0Is there a "matlablike" way to find where those segments<br>
> are? =A0Ideally, I'd like the index of the first element and the last<br>
> element ("n" is variable within the vector) but would settle (beggars<br>
> can't be choosers :) for either first or last. =A0Also ideally, it shoul=<br>
d<br>
> handle NaN, +inf and inf as well as finite numbers.<br>
><br>
> TIA,<br>
> Bruce<br>
><br>
> <br>
> +++<br>
> Bruce Bowler =A0 =A0 =A0 =A0 Experience is a good school, but the fees a=<br>
re high.<br>
> 1.207.633.9600 =A0 =A0 =A0  Heine =A0<br>
> bbow...@bigelow.org <br>
> +++<br>
<br>
=<br>
<br>
Use the diff() command. Look for where the difference is equal to<br>
zero. That will be the (start+1) or middle of your runs. At each of<br>
those starting points, scan for as long as the difference equals zero<br>
to find your ending point of the run.

Mon, 13 Apr 2009 19:01:02 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642361
Steve Amphlett
Bruce Bowler <bbowler@bigelow.org> wrote in message <74hctpF13bmmpU1@mid.individual.net>...<br>
> Sorry for the notsoconcise subject, I couldn't come up with one in less <br>
> than 10 words...<br>
> <br>
> I have a vector that's some 400,000 elements long. Periodically in this <br>
> vector there are segments where there are "n" elements in a row that are <br>
> identical. Is there a "matlablike" way to find where those segments <br>
> are? Ideally, I'd like the index of the first element and the last <br>
> element ("n" is variable within the vector) but would settle (beggars <br>
> can't be choosers :) for either first or last. Also ideally, it should <br>
> handle NaN, +inf and inf as well as finite numbers.<br>
<br>
I did one like this a while back, but it's Easter hols, so no ML at home and a pile of "stuff" tomorrow.<br>
<br>
The basic tool here is sparse. Your vector can be cut into chunks with diff. Whenever diff is nonzero, 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 oneliner.

Tue, 14 Apr 2009 16:59:02 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642649
Tim Davis
Bruce Bowler <bbowler@bigelow.org> wrote in message <74hctpF13bmmpU1@mid.individual.net>...<br>
> Sorry for the notsoconcise subject, I couldn't come up with one in less <br>
> than 10 words...<br>
> <br>
> I have a vector that's some 400,000 elements long. Periodically in this <br>
> vector there are segments where there are "n" elements in a row that are <br>
> identical. Is there a "matlablike" way to find where those segments <br>
> are? Ideally, I'd like the index of the first element and the last <br>
> element ("n" is variable within the vector) but would settle (beggars <br>
> can't be choosers :) for either first or last. Also ideally, it should <br>
> handle NaN, +inf and inf as well as finite numbers.<br>
<br>
You could create a symmetric tridiagonal sparse matrix, where A(i,i+1) = 1 if x(i) == x(i+1), with a zerofree diagonal (A(k,k)=1 for all k). Then use dmperm to find the connected components of the graph. That would be one line of M. With the r output of dmperm, the block sizes are diff(r), and you can then use "find" to find all blocks of size n. Something like<br>
<br>
n = length(x) ;<br>
s = isequalwithequalnans (x(1:n1),x(2:n)) ;<br>
A = sparse (1:n1,2:n, s, n, n) ;<br>
A = A' + speye (n) ;<br>
[p q r s] = dmperm (A) ;<br>
find(diff(r) == n)<br>
<br>
That gives you a list of all the blocks of size n.<br>
If you want the first, use find(...,'first'), or the last with find(...,'last').

Tue, 14 Apr 2009 18:38:01 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642690
Roger Stafford
Bruce Bowler <bbowler@bigelow.org> wrote in message <74hctpF13bmmpU1@mid.individual.net>...<br>
> I have a vector that's some 400,000 elements long. Periodically in this <br>
> vector there are segments where there are "n" elements in a row that are <br>
> identical. Is there a "matlablike" way to find where those segments <br>
> are? Ideally, I'd like the index of the first element and the last <br>
> element ("n" is variable within the vector) but would settle (beggars <br>
> can't be choosers :) for either first or last. Also ideally, it should <br>
> handle NaN, +inf and inf as well as finite numbers.<br>
> <br>
> TIA,<br>
> Bruce<br>
<br>
You say "'n' is variable within the vector" but you don't make it clear just how small n can be. Do you for example want the indices of just two successive equal elements? I will suppose here that for a given n you want the start and end indices of any succession of at least n elements. Let v be your given row vector.<br>
<br>
% Set n to desired value<br>
p = find([true,diff(v)~=0,true]);<br>
q = find(diff(p)>=n);<br>
x = [p(q);p(q+1)1];<br>
<br>
The first row of x will contain the first indices and the second row the last indices in v for sequences of at least n successively equal elements.<br>
<br>
This will work for inf and inf values but I'm afraid all NaNs are considered by matlab unequal to everything else, including themselves. For seqences of NaNs to be considered equal you would need a somewhat more complex code.<br>
<br>
Roger Stafford

Tue, 14 Apr 2009 19:14:02 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642699
Bruno Luong
Here is a trick to take care without much effort of Inf and NaN:<br>
<br>
% Data<br>
a=[1 1 1 inf inf 2 3 NaN NaN inf inf inf NaN]<br>
<br>
% Engine<br>
[yum yam J]=unique(typecast(a,'uint64'));<br>
first=find([true diff(J)~=0 true]);<br>
l=diff(first);<br>
% Results<br>
first=first(1:end1);<br>
last=first+l1;<br>
asplit=mat2cell(a,1,l);<br>
<br>
% Check<br>
asplit{:}<br>
<br>
% Bruno

Tue, 14 Apr 2009 19:31:03 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642700
Bruno Luong
Here is a trick to take care without much effort of Inf and NaN:<br>
<br>
% Data<br>
a=[1 1 1 inf inf 2 3 NaN NaN inf inf inf NaN]<br>
<br>
% Engine<br>
[yum yam J]=unique(typecast(a,'uint64'));<br>
first=find([true diff(J)~=0 true]);<br>
l=diff(first);<br>
% Results<br>
first=first(1:end1);<br>
last=first+l1;<br>
asplit=mat2cell(a,1,l);<br>
<br>
% Check<br>
asplit{:}<br>
<br>
% Bruno

Wed, 15 Apr 2009 14:09:41 +0000
Re: finding indentical neighbor elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/248945#642893
Bruce Bowler
On Tue, 14 Apr 2009 18:38:01 +0000, Roger Stafford wrote:<br>
<br>
> Bruce Bowler <bbowler@bigelow.org> wrote in message<br>
> <74hctpF13bmmpU1@mid.individual.net>...<br>
>> I have a vector that's some 400,000 elements long. Periodically in<br>
>> this vector there are segments where there are "n" elements in a row<br>
>> that are identical. Is there a "matlablike" way to find where those<br>
>> segments are? Ideally, I'd like the index of the first element and the<br>
>> last element ("n" is variable within the vector) but would settle<br>
>> (beggars can't be choosers :) for either first or last. Also ideally,<br>
>> it should handle NaN, +inf and inf as well as finite numbers.<br>
>> <br>
>> TIA,<br>
>> Bruce<br>
> <br>
> You say "'n' is variable within the vector" but you don't make it<br>
> clear just how small n can be. Do you for example want the indices of<br>
> just two successive equal elements? I will suppose here that for a<br>
> given n you want the start and end indices of any succession of at<br>
> least n elements. Let v be your given row vector.<br>
> <br>
> % Set n to desired value<br>
> p = find([true,diff(v)~=0,true]);<br>
> q = find(diff(p)>=n);<br>
> x = [p(q);p(q+1)1];<br>
> <br>
> The first row of x will contain the first indices and the second row the<br>
> last indices in v for sequences of at least n successively equal<br>
> elements.<br>
> <br>
> This will work for inf and inf values but I'm afraid all NaNs are<br>
> considered by matlab unequal to everything else, including themselves.<br>
> For seqences of NaNs to be considered equal you would need a somewhat<br>
> more complex code.<br>
> <br>
> Roger Stafford<br>
<br>
Roger,<br>
<br>
Thanks. The above works perfectly (well, it didn't seem to work for +/ <br>
inf, but...). As it turns out, closer inspection of the data reveals <br>
there are some values that can *never* occur, so the first pass thru the <br>
data is to set all values that aren't finite (nan, +/inf) to one of <br>
those values and then everything is "easybreezy".<br>
<br>
Bruce<br>
<br>
 <br>
+++<br>
Bruce Bowler  The oneeyed man is king in the land of the blind <br>
1.207.633.9600  Anonymous <br>
bbowler@bigelow.org  <br>
+++