<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724</link>
    <title>MATLAB Central Newsreader - Number of changes in a value</title>
    <description>Feed for thread: Number of changes in a value</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>Tue, 09 Dec 2008 17:38:02 -0500</pubDate>
      <title>Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615959</link>
      <author>anoop Sivasankaran</author>
      <description>An entry-level qestion.&lt;br&gt;
&lt;br&gt;
I have a data file (1 x 5000000), which shows different states of my model during integration. I have four states and i have used 1, 2,3,4 to indicate each state. So the entire data file contain these 4 numbers. &lt;br&gt;
&lt;br&gt;
Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3, 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&lt;br&gt;
Please let me know how to do this</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 17:58:41 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615965</link>
      <author>Walter Roberson</author>
      <description>anoop Sivasankaran wrote:&lt;br&gt;
&amp;gt; I have a data file (1 x 5000000), which shows different states of my model during integration.&lt;br&gt;
&amp;gt; I have four states and i have used 1, 2,3,4 to indicate each state.&lt;br&gt;
&lt;br&gt;
&amp;gt; Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3,&lt;br&gt;
&amp;gt; 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; Please let me know how to do this&lt;br&gt;
&lt;br&gt;
graycomatrix(TheVector,'NumLevels',4,'GrayLimits',[1 4])&lt;br&gt;
&lt;br&gt;
Note: this function might be part of the image processing toolbox&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 18:13:02 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615970</link>
      <author>Roger Stafford</author>
      <description>&quot;anoop Sivasankaran&quot; &amp;lt;anooppgd@gmail.com&amp;gt; wrote in message &amp;lt;ghmadq$egh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; An entry-level qestion.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a data file (1 x 5000000), which shows different states of my model during integration. I have four states and i have used 1, 2,3,4 to indicate each state. So the entire data file contain these 4 numbers. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3, 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Please let me know how to do this&lt;br&gt;
&lt;br&gt;
sum(diff(data)~=0)&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 18:29:03 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615978</link>
      <author>Malcolm Lidierth</author>
      <description>If you need only the total number of changes that is just&lt;br&gt;
&amp;gt;&amp;gt; sum(diff(vector));&lt;br&gt;
&lt;br&gt;
There are many ways to find specific transitions. Sticking to diff, combine with find/sort, e.g. for 3 to 4:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; vector=[1 2 3 4 5 3 2 3 4 5 6]&lt;br&gt;
vector =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2     3     4     5     3     2     3     4     5     6&lt;br&gt;
&amp;gt;&amp;gt; threes=find(vector==3)+1 % Note +1&lt;br&gt;
threes =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     7     9&lt;br&gt;
&amp;gt;&amp;gt; fours=find(vector==4)&lt;br&gt;
fours =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     9&lt;br&gt;
&amp;gt;&amp;gt; buffer=sort([threes fours])&lt;br&gt;
buffer =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     4     7     9     9&lt;br&gt;
&lt;br&gt;
To get the total:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; sum(~diff(buffer)) % Note the ~ this time&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;br&gt;
&lt;br&gt;
P.S. don't use &quot;ones&quot; as a variable name - that's an ML function</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 18:54:33 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615983</link>
      <author>Walter Roberson</author>
      <description>Roger Stafford wrote:&lt;br&gt;
&amp;gt; &quot;anoop Sivasankaran&quot; &amp;lt;anooppgd@gmail.com&amp;gt; wrote in message &amp;lt;ghmadq$egh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3,&lt;br&gt;
&amp;gt;&amp;gt; 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&lt;br&gt;
&amp;gt; sum(diff(data)~=0)&lt;br&gt;
&lt;br&gt;
Hmmm, my interpretation was that Anoop needs the counts for the cases individually, but&lt;br&gt;
certainly that isn't clear.&lt;br&gt;
&lt;br&gt;
Interesting, your formulation was the fastest of the several I tried&lt;br&gt;
at about 0.15 seconds; several variants came in about 0.16 seconds, and&lt;br&gt;
sum(data(1:end-1)~=data(2:end)) was about 0.31 seconds&lt;br&gt;
&lt;br&gt;
The graycomatrix that I suggested in a posting above takes about 4 seconds&lt;br&gt;
to produce the full 4 x 4 table.&lt;br&gt;
&lt;br&gt;
And then there is:&lt;br&gt;
&lt;br&gt;
[A,B] = meshgrid(1:4,1:4);&lt;br&gt;
arrayfun(@(a,b) sum(foo(1:end-1)==a&amp;foo(2:end)==b), A,B)&lt;br&gt;
which takes about 6 seconds for the arrayfun&lt;br&gt;
&lt;br&gt;
I couldn't seem to formulate the equivalent in bsxfun.</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 19:17:04 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615990</link>
      <author>Steve Amphlett</author>
      <description>Walter Roberson &amp;lt;roberson@hushmail.com&amp;gt; wrote in message &amp;lt;y9z%k.777$Ir1.624@newsfe05.iad&amp;gt;...&lt;br&gt;
&amp;gt; Roger Stafford wrote:&lt;br&gt;
&amp;gt; &amp;gt; &quot;anoop Sivasankaran&quot; &amp;lt;anooppgd@gmail.com&amp;gt; wrote in message &amp;lt;ghmadq$egh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3,&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; sum(diff(data)~=0)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hmmm, my interpretation was that Anoop needs the counts for the cases individually, but&lt;br&gt;
&amp;gt; certainly that isn't clear.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Interesting, your formulation was the fastest of the several I tried&lt;br&gt;
&amp;gt; at about 0.15 seconds; several variants came in about 0.16 seconds, and&lt;br&gt;
&amp;gt; sum(data(1:end-1)~=data(2:end)) was about 0.31 seconds&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The graycomatrix that I suggested in a posting above takes about 4 seconds&lt;br&gt;
&amp;gt; to produce the full 4 x 4 table.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; And then there is:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [A,B] = meshgrid(1:4,1:4);&lt;br&gt;
&amp;gt; arrayfun(@(a,b) sum(foo(1:end-1)==a&amp;foo(2:end)==b), A,B)&lt;br&gt;
&amp;gt; which takes about 6 seconds for the arrayfun&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I couldn't seem to formulate the equivalent in bsxfun.&lt;br&gt;
&lt;br&gt;
This looks like a problem for sparse() to solve .  Each pair of consecutive states (values) could be indices into a sparse 2D array that gets fed with ones and adds them up.  Not having ML at home, I thought I'd at least share this idea so that someone could maybe finish it off.&lt;br&gt;
&lt;br&gt;
% Excuse syntax, probably wrong, but the right idea&lt;br&gt;
x=sparse(data(1:end-1),data(2:end),ones(length(data)-1),1);&lt;br&gt;
&lt;br&gt;
... or something along those lines.  Then zero the diagonal.  The result would be a 4x4 sparse matrix which each (row,col) containing the number of transitions from (row) to (col).</description>
    </item>
    <item>
      <pubDate>Tue, 09 Dec 2008 19:45:05 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#615999</link>
      <author>Adam </author>
      <description>&quot;anoop Sivasankaran&quot; &amp;lt;anooppgd@gmail.com&amp;gt; wrote in message &amp;lt;ghmadq$egh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; An entry-level qestion.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a data file (1 x 5000000), which shows different states of my model during integration. I have four states and i have used 1, 2,3,4 to indicate each state. So the entire data file contain these 4 numbers. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3, 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&lt;br&gt;
This takes 0.8 sec on my machine&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;test = randi(4, [1, 5e6]);  % test vector&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mask = [1 2 4 8];           % map values&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% each transition now has unique value, bin it&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result = hist(diff(mask(test)), -7:7);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% generate output mask to map values into 4x4 array&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp = meshgrid(mask);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outMask = temp-temp' + 8;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result(outMask)&lt;br&gt;
&lt;br&gt;
I didn't do anything with the diagonal because it sounds like you ignore it anyway.&lt;br&gt;
&lt;br&gt;
~Adam</description>
    </item>
    <item>
      <pubDate>Wed, 10 Dec 2008 10:29:02 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#616129</link>
      <author>Jos </author>
      <description>&quot;Steve Amphlett&quot; &amp;lt;Firstname.Lastname@Where-I-Work.com&amp;gt; wrote in message &amp;lt;ghmg7g$ic6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Walter Roberson &amp;lt;roberson@hushmail.com&amp;gt; wrote in message &amp;lt;y9z%k.777$Ir1.624@newsfe05.iad&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Roger Stafford wrote:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &quot;anoop Sivasankaran&quot; &amp;lt;anooppgd@gmail.com&amp;gt; wrote in message &amp;lt;ghmadq$egh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; Now I need to find number of changes. i.e I need to find how many changes from 1 to 2, 1 to 3,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; 1 to 4, 2 to 1, 2 to 3 etc. &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; sum(diff(data)~=0)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Hmmm, my interpretation was that Anoop needs the counts for the cases individually, but&lt;br&gt;
&amp;gt; &amp;gt; certainly that isn't clear.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Interesting, your formulation was the fastest of the several I tried&lt;br&gt;
&amp;gt; &amp;gt; at about 0.15 seconds; several variants came in about 0.16 seconds, and&lt;br&gt;
&amp;gt; &amp;gt; sum(data(1:end-1)~=data(2:end)) was about 0.31 seconds&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The graycomatrix that I suggested in a posting above takes about 4 seconds&lt;br&gt;
&amp;gt; &amp;gt; to produce the full 4 x 4 table.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; And then there is:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; [A,B] = meshgrid(1:4,1:4);&lt;br&gt;
&amp;gt; &amp;gt; arrayfun(@(a,b) sum(foo(1:end-1)==a&amp;foo(2:end)==b), A,B)&lt;br&gt;
&amp;gt; &amp;gt; which takes about 6 seconds for the arrayfun&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I couldn't seem to formulate the equivalent in bsxfun.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This looks like a problem for sparse() to solve .  Each pair of consecutive states (values) could be indices into a sparse 2D array that gets fed with ones and adds them up.  Not having ML at home, I thought I'd at least share this idea so that someone could maybe finish it off.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Excuse syntax, probably wrong, but the right idea&lt;br&gt;
&amp;gt; x=sparse(data(1:end-1),data(2:end),ones(length(data)-1),1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ... or something along those lines.  Then zero the diagonal.  The result would be a 4x4 sparse matrix which each (row,col) containing the number of transitions from (row) to (col).&lt;br&gt;
&lt;br&gt;
Indeed, sparse is the way to go:&lt;br&gt;
&lt;br&gt;
V = [1 2 3 2 4 1 4 2 3 1 4 2 3 2 1 3 2 4 3 2 3 4 2 3 4 1 2 1 2 1] ;&lt;br&gt;
A = full(sparse(V(1:end-1),V(2:end),1))&lt;br&gt;
&lt;br&gt;
so that A(i,j) holds the number of changes from i to j&lt;br&gt;
&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Wed, 10 Dec 2008 15:44:04 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#616177</link>
      <author>anoop Sivasankaran</author>
      <description>&lt;br&gt;
&lt;br&gt;
&amp;gt; Indeed, sparse is the way to go:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; V = [1 2 3 2 4 1 4 2 3 1 4 2 3 2 1 3 2 4 3 2 3 4 2 3 4 1 2 1 2 1] ;&lt;br&gt;
&amp;gt; A = full(sparse(V(1:end-1),V(2:end),1))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; so that A(i,j) holds the number of changes from i to j&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Jos&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;nbsp;Thank you, Sparse is probably the easyest way to do it. What are the diagonal elements? </description>
    </item>
    <item>
      <pubDate>Wed, 10 Dec 2008 15:48:02 -0500</pubDate>
      <title>Re: Number of changes in a value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240724#616182</link>
      <author>Jos </author>
      <description>&quot;anoop Sivasankaran&quot; &amp;lt;anooppgd@gmail.com&amp;gt; wrote in message &amp;lt;ghoo44$e2f$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Indeed, sparse is the way to go:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; V = [1 2 3 2 4 1 4 2 3 1 4 2 3 2 1 3 2 4 3 2 3 4 2 3 4 1 2 1 2 1] ;&lt;br&gt;
&amp;gt; &amp;gt; A = full(sparse(V(1:end-1),V(2:end),1))&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; so that A(i,j) holds the number of changes from i to j&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Jos&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt;  Thank you, Sparse is probably the easyest way to do it. What are the diagonal elements? &lt;br&gt;
&lt;br&gt;
&quot;changes&quot; from state K to state K ... &lt;br&gt;
&lt;br&gt;
Jos</description>
    </item>
  </channel>
</rss>

