Re: number of switches

*To*: mathgroup at smc.vnet.net*Subject*: [mg47523] Re: number of switches*From*: "Peltio" <peltio at twilight.zone>*Date*: Thu, 15 Apr 2004 03:39:35 -0400 (EDT)*References*: <c5j6vd$qqo$1@smc.vnet.net>*Reply-to*: "Peltio" <peltioNOSP at Mdespammed.com.invalid>*Sender*: owner-wri-mathgroup at wolfram.com

"fake" wrote >Consider the lists {1,1,0,1} and {1,1,0,0},{1,0,1,0,1}. >The first sequence (1101) switches 2 times (#2digit~#3digit, >#3digit~#4digit}, the second (1100) 1 time, the third 10101 4 times. >I have the following problem. >Consider a list of binary digits. Which is the easiest way to count the >number of switches of the list (using Mathematica commands)? Since we're working wit 1's and 0's the difference between two values can only be 0 (if they are the same, no switch), +1 (0->1) or -1 (1->0). Subtracting the list of bits from a rotated version (the last element is adjusted in order not to give a false switch), we get a list of such transition values. Applying Abs will turn every switch into a 1. To count them all, all we need to to is a simple sum: edgeNumber[lst_] := Plus @@ Abs[Append[Rest[lst], Last[lst]] - lst] Some examples: edgeNumber[{1,1,0,1}] 2 edgeNumber[{1,1,0,0] 1 edgeNumber[{1,0,1,0,1}] 4 edgeNumber[{1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1}] 8 But if you have access to a version of Mathematica that has Split built-in (from 3.0 onward) the simplest way should be this: edgeNumber[lst_]=Length[Split[lst]]-1 It works for multilevel signals as well. cheers, Peltio invalid address in reply-to crafty demunging required to mail me