<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237218</link>
    <title>MATLAB Central Newsreader - Count state switches in a vector?</title>
    <description>Feed for thread: Count state switches in a vector?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 09 Oct 2008 13:53:13 -0400</pubDate>
      <title>Count state switches in a vector?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237218#604369</link>
      <author>Ian Clarkson</author>
      <description>Hi there,&lt;br&gt;
&lt;br&gt;
I'm trying to find a smart (read: vectorized) way to count &quot;state switches&quot; in a vector. What I mean is, the number of times a vector switches between 0 and 1:&lt;br&gt;
&lt;br&gt;
[0 1 0] -&amp;gt; 2&lt;br&gt;
[1 0 1] -&amp;gt; 2&lt;br&gt;
[0 0 1] -&amp;gt; 1&lt;br&gt;
[0 0 0 1 1 0 0 1 1] -&amp;gt; 3&lt;br&gt;
[1 1 1 1 1 1 1 1 0] -&amp;gt; 1&lt;br&gt;
&lt;br&gt;
The solution only really needs to take a vector of length 32, and it will always be a value of either 0 or 1. I can obviously do this in a loop, but I was wondering if there was some clever function I could use to do this for a really big set of these vectors. Maybe something about a cumulative summation or something?&lt;br&gt;
&lt;br&gt;
Thanks!</description>
    </item>
    <item>
      <pubDate>Thu, 09 Oct 2008 16:43:45 -0400</pubDate>
      <title>Re: Count state switches in a vector?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237218#604474</link>
      <author>Walter Roberson</author>
      <description>Ian Clarkson wrote:&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; I'm trying to find a smart (read: vectorized) way to count &quot;state switches&quot; in a vector.&lt;br&gt;
&lt;br&gt;
&amp;gt; [0 0 0 1 1 0 0 1 1] -&amp;gt; 3&lt;br&gt;
&lt;br&gt;
&amp;gt; The solution only really needs to take a vector of length 32, and it will always be a value&lt;br&gt;
&amp;gt; of either 0 or 1.&lt;br&gt;
&lt;br&gt;
sum(abs(diff([0 0 0 1 1 0 0 1 1])))&lt;br&gt;
&lt;br&gt;
OR&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; A = [0 0 0 1 1 0 0 1 1];&lt;br&gt;
&amp;gt;&amp;gt; sum(A(1:end-1) ~= A(2:end))</description>
    </item>
    <item>
      <pubDate>Thu, 09 Oct 2008 16:44:53 -0400</pubDate>
      <title>Re: Count state switches in a vector?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237218#604475</link>
      <author>Walter Roberson</author>
      <description>Walter Roberson wrote:&lt;br&gt;
&amp;gt; Ian Clarkson wrote:&lt;br&gt;
&amp;gt;&amp;gt; I'm trying to find a smart (read: vectorized) way to count &quot;state &lt;br&gt;
&amp;gt;&amp;gt; switches&quot; in a vector.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; A = [0 0 0 1 1 0 0 1 1];&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; sum(A(1:end-1) ~= A(2:end))&lt;br&gt;
&lt;br&gt;
Or, since you are only using 0 and 1,&lt;br&gt;
&lt;br&gt;
sum(xor(A(1:end-1),A(2:end)))</description>
    </item>
  </channel>
</rss>

