MathGroup Archive 2006

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

Search the Archive

Re: finding the position of a pattern in list


In article <dtm5j1$hf3$1 at smc.vnet.net>, dh <dh at metrohm.ch> wrote:

> a simple solution would be to subtract adjacent elemnts and then search 
> for +1.
> E.g.:
> 
> d = {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
> t = Drop[d, 1] - Drop[d, -1]
> Position[t, 1] // Flatten

A slight improvement is

  d = {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
  Position[ListConvolve[{1, -1}, d], 1] // Flatten

This is faster than solutions using Partition.

Cheers,
Paul

_______________________________________________________________________
Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul


  • Prev by Date: solid of revolution
  • Next by Date: Re: Limit
  • Previous by thread: Re: finding the position of a pattern in list
  • Next by thread: Multivariable Limits