MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Fast List-Selection


Hans Havermann  wrote:
---------------------------

I have a list 's' composed of a large number of (small) integers. I wish to
search this list for instances of 7 consecutive, identical elements.

My approach is:

Do[If[Count[t = Take[s, {i, i + 6}], t[[1]]] == 7,
    Print[i]], {i, 1, Length[s] - 6}]

Can anyone think of a *faster* way of doing this?

--------------------

Split is made for this sort of thing.  See the usage 
message and example below.  I would give code to solve 
the very problem you describe, but I am having a hard 
time understanding what the code should do.  Anyway, 
using Split should make you code run much faster.


In[1]:=
?Split

Split[list] splits list into sublists consisting of 
runs of identical elements. Split[list, test] treats 
pairs of adjacent elements as identical whenever 
applying the function test to them yields True.



In[2]:=
s=Table[Random[Integer,2],{100}]

Out[2]=
{2,2,0,0,1,0,1,2,2,0,1,0,0,2,0,0,0,0,2,1,2,0,1,0,0,
0,0,2,2,2,1,1,1,2,1,2,1,0,0,2,2,2,2,0,1,1,1,2,1,0,2,
2,2,1,1,2,2,2,2,1,1,2,1,1,1,1,1,1,2,0,2,1,1,0,0,1,2,
0,1,0,0,0,2,0,0,2,0,2,2,1,1,0,2,2,1,2,1,1,2,2}



In[3]:=
tt=Split[s]

Out[3]=
{{2,2},{0,0},{1},{0},{1},{2,2},{0},{1},{0,0},{2},
{0,0,0,0},{2},{1},{2},{0},{1},{0,0,0,0},{2,2,2},
{1,1,1},{2},{1},{2},{1},{0,0},{2,2,2,2},{0},{1,1,1},
{2},{1},{0},{2,2,2},{1,1},{2,2,2,2},{1,1},{2},
{1,1,1,1,1,1},{2},{0},{2},{1,1},{0,0},{1},{2},{0},
{1},{0,0,0},{2},{0,0},{2},{0},{2,2},{1,1},{0},{2,2},
{1},{2},{1,1},{2,2}}


--------------------
Regards,
Ted Ersek

For Mathematica tips, tricks see 
http://www.dot.net.au/~elisha/ersek/Tricks.html


  • Prev by Date: Re: Fast List-Selection
  • Next by Date: RE: Does Mathematica work reliably in an Overclocked system
  • Previous by thread: Re: Fast List-Selection
  • Next by thread: Re: Fast List-Selection