Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news2.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe01.iad.POSTED!7564ea0f!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
Organization: Canada Eat The Cookie Foundation
User-Agent: Thunderbird 2.0.0.17 (Windows/20080914)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: Count state switches in a vector?
References: <gcl2c9$hp$1@fred.mathworks.com>
In-Reply-To: <gcl2c9$hp$1@fred.mathworks.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 15
Message-ID: <kwqHk.15470$1Z4.8388@newsfe01.iad>
NNTP-Posting-Host: 24.79.146.116
X-Complaints-To: internet.abuse@sjrb.ca
X-Trace: newsfe01.iad 1223570576 24.79.146.116 (Thu, 09 Oct 2008 16:42:56 UTC)
NNTP-Posting-Date: Thu, 09 Oct 2008 16:42:56 UTC
Date: Thu, 09 Oct 2008 11:43:45 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:494393


Ian Clarkson wrote:
 
> I'm trying to find a smart (read: vectorized) way to count "state switches" in a vector.

> [0 0 0 1 1 0 0 1 1] -> 3

> The solution only really needs to take a vector of length 32, and it will always be a value
> of either 0 or 1.

sum(abs(diff([0 0 0 1 1 0 0 1 1])))

OR

>> A = [0 0 0 1 1 0 0 1 1];
>> sum(A(1:end-1) ~= A(2:end))