Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!news2.glorb.com!postnews.google.com!f1g2000prb.googlegroups.com!not-for-mail
From: andywocky@gmail.com
Newsgroups: comp.soft-sys.matlab
Subject: Re: Find quantity of number changes in a vector
Date: Tue, 24 Mar 2009 16:55:09 -0700 (PDT)
Organization: http://groups.google.com
Lines: 16
Message-ID: <36327e39-a39a-48f2-8eb2-8f65fa17069d@f1g2000prb.googlegroups.com>
References: <28f55bdc-43ce-4c76-a1eb-ff9d4a5483f4@z15g2000yqm.googlegroups.com>
NNTP-Posting-Host: 208.73.31.70
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1237938909 26687 127.0.0.1 (24 Mar 2009 23:55:09 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 24 Mar 2009 23:55:09 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: f1g2000prb.googlegroups.com; posting-host=208.73.31.70; 
	posting-account=JBJwVAkAAABIr1xPZwnADOjGj5e0x7mJ
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; 
	rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:527444

On Mar 24, 3:48=A0pm, quintes...@googlemail.com wrote:
> Hi,
> Apologies if this is a newb question. =A0I have a vector containing
> numbers: 0 0 0 1 1 1 1 0 0 0 -1 -1 -1 -1 0 0 0
> I want to count the number changes, eg above would be 4 because 0 to 1
> to 0 to -1 then forthly back to 0. =A0I could achieve using a for loop,
> but would prefer a less computationally intensive solution.
>
> Many thanks.

... looks like the solution from "us" is faster.  Median execution
time on my computer for five different runs are:

   sum(abs(diff(v)))   ~ 2.72 microseconds
   nnz(diff(v))            ~ 6.00 microseconds
   nnz(diff(v)~=3D0)      ~ 8.20 microseconds