Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Counting Runs

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51932] Re: [mg51890] Counting Runs
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Fri, 5 Nov 2004 02:17:49 -0500 (EST)
  • References: <200411040650.BAA18131@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Nov 4, 2004, at 1:50 AM, Gregory Lypny wrote:

> 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.
>

I'm not sure about elegance but:

With[{lst=#[[1]]&/@Split[v]},{#,Count[lst,#]}&/@Union[lst]]

will do what you want.

Regards,

Ssezi


  • References:
  • Prev by Date: Adding Vectors -- Newbie help please
  • Next by Date: Re: foreach loop
  • Previous by thread: Re: Counting Runs
  • Next by thread: Re: Counting Runs