MathGroup Archive 2004

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

Search the Archive

RE: number of switches


>-----Original Message-----
>From: fake [mailto:fake at fake.it]
To: mathgroup at smc.vnet.net
>Sent: Wednesday, April 14, 2004 1:16 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg47527] [mg47479] number of switches
>
>
>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)?
>

In[1]:= tt = Table[Random[Integer], {30}]
Out[1]=
{0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0,
\
1, 0, 1, 1, 0}


In[3]:= Length[Split[tt]] - 1
Out[3]= 16

In[10]:= Plus @@ Abs /@ Subtract @@@ Partition[tt, 2, 1]
Out[10]= 16

In[68]:=
(t = FromDigits[tt], 2]; 
  DigitCount[BitXor[Floor[t/2], t], 2, 1] - First[tt])
Out[68]= 16


--
Hartmut Wolf


  • Prev by Date: Re: number of switches
  • Next by Date: Re: number of switches
  • Previous by thread: Re: number of switches
  • Next by thread: Re: number of switches