Re: Detect pairs, tripples in a list
- To: mathgroup at smc.vnet.net
- Subject: [mg22451] Re: [mg22442] Detect pairs, tripples in a list
- From: Rob Pratt <rpratt at email.unc.edu>
- Date: Wed, 8 Mar 2000 02:21:50 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
If you want to leave longer runs of ones alone, the following code will do
the job:
Flatten[Split[lis]/.{{1,1}->{1,0},{1,1,1}->{1,0,0}}]
If you want to replace ALL runs of ones, use this instead:
Flatten[Split[lis]/.{{1,x___}:>Prepend[Table[0,{Length[{x}]}],1]}]
Rob Pratt
Department of Operations Research
The University of North Carolina at Chapel Hill
rpratt at email.unc.edu
http://www.unc.edu/~rpratt/
On Sat, 4 Mar 2000, Hans Friedrich Steffani wrote:
> I have a list with many 0s and some 1s. Now I want to detect pairs
> and tripples of 1s with no 0 between to substitute them by a single 1
> and the apropriate number of 0. How can this be done?
>
> Hans Friedrich Steffani
>
> --
> Hans Friedrich Steffani
> Institut fuer Elektrische Maschinen und Antriebe, TU Chemnitz
> mailto:hans.steffani at e-technik.tu-chemnitz.de
> http://www.tu-chemnitz.de/~hfst/