Re: Counting Runs

*To*: mathgroup at smc.vnet.net*Subject*: [mg51946] Re: [mg51890] Counting Runs*From*: "David Park" <djmp at earthlink.net>*Date*: Fri, 5 Nov 2004 02:18:50 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Gregory, Here is a method in some detail. data = {1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1}; Print["Split data into runs"] Split[data] Print["Extract the first element of each run"] First /@ %% Print["Sort and split the runs of each element"] Split@Sort@%% Print["Print the results"] Print["Number of runs of ", First[#], " is ", Length[#]] & /@ %%; David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Gregory Lypny [mailto:gregory.lypny at videotron.ca] To: mathgroup at smc.vnet.net Looking for an elegant way to count runs to numbers in a series. Suppose I have a list of ones and negative ones such as v={1,1,1,-1,1,1,1,1,1,-1,-1,-1,-1,1}. I'd like to create a function that counts the number of runs of 1s and -1s, which in this case is 3 and 2. Greg