<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813</link>
    <title>MATLAB Central Newsreader - finding consecutive numbers (runs)</title>
    <description>Feed for thread: finding consecutive numbers (runs)</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The 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>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 12 Dec 2007 18:37:39 -0500</pubDate>
      <title>finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#405923</link>
      <author>William Dampier</author>
      <description>I am trying to find a speedy way to find the number of&lt;br&gt;
consecutive 1's in a vector of logicals in with the&lt;br&gt;
following type of output.&lt;br&gt;
&lt;br&gt;
input =    [1 0 1 1 1 0 1 1 0 0 0 1 0 1]&lt;br&gt;
desired1 = [1 0 3 2 1 0 2 1 0 0 0 1 0 1]&lt;br&gt;
desired2 = [1 0 1 2 3 0 1 2 0 0 0 1 0 1] %the reverse direction&lt;br&gt;
&lt;br&gt;
desired1=zeros(size(input));&lt;br&gt;
spots=find(input);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;desired1(1:spots(1))=(spots(1)-1):-1:0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k=2:length(spots)-1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
desired1(spots(k-1):spots(k))=(spots(k)-spots(k-1)):-1:0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I understand to avoid for-loops but I didn't see a way&lt;br&gt;
around it in this case.  The real input is actually a matrix&lt;br&gt;
of logicals in which I want the to do apply this to each row&lt;br&gt;
individually (so if you see a vectorized solution I would&lt;br&gt;
certainly be appreciative).  Currently I find desired2 using&lt;br&gt;
the same algorithm on fliplr(input).&lt;br&gt;
&lt;br&gt;
real_input =   [1 0 1 1 1 0 0 1 0 0 1 1 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0 1 0 0 1 1 1 0 1 1 0 0 0];&lt;br&gt;
real_desired = [1 0 3 2 1 0 0 1 0 0 2 1 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0 1 0 0 3 2 1 0 2 1 0 0 0];&lt;br&gt;
&lt;br&gt;
The real data is also a ~6000 X 9000 matrix so I'd like to&lt;br&gt;
avoid creating too many temporary arrays.&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 12 Dec 2007 21:06:58 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#405948</link>
      <author>Roger Stafford</author>
      <description>"William Dampier" &amp;lt;walldo2@gmail.com&amp;gt; wrote in message &amp;lt;fjp9pj$p8g&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I am trying to find a speedy way to find the number of&lt;br&gt;
&amp;gt; consecutive 1's in a vector of logicals in with the&lt;br&gt;
&amp;gt; following type of output.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; input =    [1 0 1 1 1 0 1 1 0 0 0 1 0 1]&lt;br&gt;
&amp;gt; desired1 = [1 0 3 2 1 0 2 1 0 0 0 1 0 1]&lt;br&gt;
&amp;gt; desired2 = [1 0 1 2 3 0 1 2 0 0 0 1 0 1] %the reverse direction&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; desired1=zeros(size(input));&lt;br&gt;
&amp;gt; spots=find(input);&lt;br&gt;
&amp;gt;     desired1(1:spots(1))=(spots(1)-1):-1:0;&lt;br&gt;
&amp;gt;     for k=2:length(spots)-1&lt;br&gt;
&amp;gt;        &lt;br&gt;
&amp;gt; desired1(spots(k-1):spots(k))=(spots(k)-spots(k-1)):-1:0;&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I understand to avoid for-loops but I didn't see a way&lt;br&gt;
&amp;gt; around it in this case.  The real input is actually a matrix&lt;br&gt;
&amp;gt; of logicals in which I want the to do apply this to each row&lt;br&gt;
&amp;gt; individually (so if you see a vectorized solution I would&lt;br&gt;
&amp;gt; certainly be appreciative).  Currently I find desired2 using&lt;br&gt;
&amp;gt; the same algorithm on fliplr(input).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; real_input =   [1 0 1 1 1 0 0 1 0 0 1 1 0;&lt;br&gt;
&amp;gt;                 0 1 0 0 1 1 1 0 1 1 0 0 0];&lt;br&gt;
&amp;gt; real_desired = [1 0 3 2 1 0 0 1 0 0 2 1 0;&lt;br&gt;
&amp;gt;                 0 1 0 0 3 2 1 0 2 1 0 0 0];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The real data is also a ~6000 X 9000 matrix so I'd like to&lt;br&gt;
&amp;gt; avoid creating too many temporary arrays.&lt;br&gt;
-----------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;If x is an m by n matrix of your inputs, then the following y matrix below &lt;br&gt;
should be the 'desired2' matrix you requested.  The 'desired1' matrix can be &lt;br&gt;
obtained by repeating the same process using fliplr(x) in place of x (the &lt;br&gt;
reverse direction of what you mentioned.)  I was too lazy to try for a &lt;br&gt;
simultaneous solution of both matrices.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[m,n] = size(x);&lt;br&gt;
&amp;nbsp;y = [zeros(1,m);x.'];&lt;br&gt;
&amp;nbsp;y = y(:);&lt;br&gt;
&amp;nbsp;p = find(~y);&lt;br&gt;
&amp;nbsp;y(p) = [0;1-diff(p)];&lt;br&gt;
&amp;nbsp;y = reshape(cumsum(y),[],m).';&lt;br&gt;
&amp;nbsp;y(:,1) = [];&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 12 Dec 2007 21:59:30 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#405955</link>
      <author>William Dampier</author>
      <description>"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt;&lt;br&gt;
wrote in message &amp;lt;fjpihi$p2j$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "William Dampier" &amp;lt;walldo2@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fjp9pj$p8g&lt;br&gt;
&amp;gt; $1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I am trying to find a speedy way to find the number of&lt;br&gt;
&amp;gt; &amp;gt; consecutive 1's in a vector of logicals in with the&lt;br&gt;
&amp;gt; &amp;gt; following type of output.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; input =    [1 0 1 1 1 0 1 1 0 0 0 1 0 1]&lt;br&gt;
&amp;gt; &amp;gt; desired1 = [1 0 3 2 1 0 2 1 0 0 0 1 0 1]&lt;br&gt;
&amp;gt; &amp;gt; desired2 = [1 0 1 2 3 0 1 2 0 0 0 1 0 1] %the reverse&lt;br&gt;
direction&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; desired1=zeros(size(input));&lt;br&gt;
&amp;gt; &amp;gt; spots=find(input);&lt;br&gt;
&amp;gt; &amp;gt;     desired1(1:spots(1))=(spots(1)-1):-1:0;&lt;br&gt;
&amp;gt; &amp;gt;     for k=2:length(spots)-1&lt;br&gt;
&amp;gt; &amp;gt;        &lt;br&gt;
&amp;gt; &amp;gt; desired1(spots(k-1):spots(k))=(spots(k)-spots(k-1)):-1:0;&lt;br&gt;
&amp;gt; &amp;gt;     end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I understand to avoid for-loops but I didn't see a way&lt;br&gt;
&amp;gt; &amp;gt; around it in this case.  The real input is actually a matrix&lt;br&gt;
&amp;gt; &amp;gt; of logicals in which I want the to do apply this to each row&lt;br&gt;
&amp;gt; &amp;gt; individually (so if you see a vectorized solution I would&lt;br&gt;
&amp;gt; &amp;gt; certainly be appreciative).  Currently I find desired2 using&lt;br&gt;
&amp;gt; &amp;gt; the same algorithm on fliplr(input).&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; real_input =   [1 0 1 1 1 0 0 1 0 0 1 1 0;&lt;br&gt;
&amp;gt; &amp;gt;                 0 1 0 0 1 1 1 0 1 1 0 0 0];&lt;br&gt;
&amp;gt; &amp;gt; real_desired = [1 0 3 2 1 0 0 1 0 0 2 1 0;&lt;br&gt;
&amp;gt; &amp;gt;                 0 1 0 0 3 2 1 0 2 1 0 0 0];&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The real data is also a ~6000 X 9000 matrix so I'd like to&lt;br&gt;
&amp;gt; &amp;gt; avoid creating too many temporary arrays.&lt;br&gt;
&amp;gt; -----------------&lt;br&gt;
&amp;gt;   If x is an m by n matrix of your inputs, then the&lt;br&gt;
following y matrix below &lt;br&gt;
&amp;gt; should be the 'desired2' matrix you requested.  The&lt;br&gt;
'desired1' matrix can be &lt;br&gt;
&amp;gt; obtained by repeating the same process using fliplr(x) in&lt;br&gt;
place of x (the &lt;br&gt;
&amp;gt; reverse direction of what you mentioned.)  I was too lazy&lt;br&gt;
to try for a &lt;br&gt;
&amp;gt; simultaneous solution of both matrices.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  [m,n] = size(x);&lt;br&gt;
&amp;gt;  y = [zeros(1,m);x.'];&lt;br&gt;
&amp;gt;  y = y(:);&lt;br&gt;
&amp;gt;  p = find(~y);&lt;br&gt;
&amp;gt;  y(p) = [0;1-diff(p)];&lt;br&gt;
&amp;gt;  y = reshape(cumsum(y),[],m).';&lt;br&gt;
&amp;gt;  y(:,1) = [];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Thanks a whole lot.&lt;br&gt;
&lt;br&gt;
It took me a few minutes to realize that if I fliplr(input)&lt;br&gt;
then I'll also have to fliplr the output as well. like this:&lt;br&gt;
&lt;br&gt;
input=[1 0 1 1 1 0 1 1 0 0 0 1 0 1];&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
x=input;&lt;br&gt;
[m,n] = size(x);&lt;br&gt;
y = [zeros(1,m);x.'];&lt;br&gt;
y = y(:);&lt;br&gt;
p = find(~y);&lt;br&gt;
y(p) = [0;1-diff(p)];&lt;br&gt;
y = reshape(cumsum(y),[],m).';&lt;br&gt;
y(:,1) = [];&lt;br&gt;
&lt;br&gt;
x=fliplr(input);&lt;br&gt;
[m,n] = size(x);&lt;br&gt;
y2 = [zeros(1,m);x.'];&lt;br&gt;
y2 = y2(:);&lt;br&gt;
p = find(~y2);&lt;br&gt;
y2(p) = [0;1-diff(p)];&lt;br&gt;
y2 = reshape(cumsum(y2),[],m).';&lt;br&gt;
y2(:,1) = [];&lt;br&gt;
&lt;br&gt;
desired=[input; y; fliplr(y2)]&lt;br&gt;
&lt;br&gt;
This shaves more than 40 seconds off of each of ~3000&lt;br&gt;
iterations (dividing training/testing dataset) so the final&lt;br&gt;
speed up is very large!&lt;br&gt;
&lt;br&gt;
Thanks again&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 13 Dec 2007 05:17:55 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#405991</link>
      <author>Roger Stafford</author>
      <description>"William Dampier" &amp;lt;walldo2@gmail.com&amp;gt; wrote in message &amp;lt;fjplk2$fgm&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt;&lt;br&gt;
&amp;gt; wrote in message &amp;lt;fjpihi$p2j$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; .......&lt;br&gt;
&amp;gt; &amp;gt;  [m,n] = size(x);&lt;br&gt;
&amp;gt; &amp;gt;  y = [zeros(1,m);x.'];&lt;br&gt;
&amp;gt; &amp;gt;  y = y(:);&lt;br&gt;
&amp;gt; &amp;gt;  p = find(~y);&lt;br&gt;
&amp;gt; &amp;gt;  y(p) = [0;1-diff(p)];&lt;br&gt;
&amp;gt; &amp;gt;  y = reshape(cumsum(y),[],m).';&lt;br&gt;
&amp;gt; &amp;gt;  y(:,1) = [];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks a whole lot.&lt;br&gt;
&amp;gt; It took me a few minutes to realize that if I fliplr(input)&lt;br&gt;
&amp;gt; then I'll also have to fliplr the output as well. like this:&lt;br&gt;
&amp;gt; .......&lt;br&gt;
--------&lt;br&gt;
&amp;nbsp;&amp;nbsp;William, it has occurred to me that the 'fliplr' operations are not really &lt;br&gt;
needed at all in the above processing if the appropriate steps are taken.  &lt;br&gt;
Besides these two 'fliplr' operations, one 'diff' and one 'find' operation can &lt;br&gt;
also be eliminated.  Consequently you may find that, hopefully, a little &lt;br&gt;
execution time will be saved.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Again, if x is the m by n array whose rows are to be processed in the &lt;br&gt;
specified manner, then do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;m = size(x,1);&lt;br&gt;
&amp;nbsp;y = [reshape([zeros(1,m);x.'],[],1);0];&lt;br&gt;
&amp;nbsp;z = y;&lt;br&gt;
&amp;nbsp;p = find(~y);&lt;br&gt;
&amp;nbsp;d = 1-diff(p);&lt;br&gt;
&amp;nbsp;y(p) = [0;d];&lt;br&gt;
&amp;nbsp;y = reshape(cumsum(y(1:end-1)),[],m).';&lt;br&gt;
&amp;nbsp;y(:,1) = [];&lt;br&gt;
&amp;nbsp;z(p) = [d;0];&lt;br&gt;
&amp;nbsp;z = reshape(cumsum(-z(1:end-1)),[],m).';&lt;br&gt;
&amp;nbsp;z(:,end) = [];&lt;br&gt;
&lt;br&gt;
The y array will then be the above-mentioned 'desired2' array, and z will be &lt;br&gt;
the 'desired1' array, both of size m by n.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 13 Dec 2007 06:53:40 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#406000</link>
      <author>William Dampier</author>
      <description>&amp;gt;   William, it has occurred to me that the 'fliplr'&lt;br&gt;
operations are not really &lt;br&gt;
&amp;gt; needed at all in the above processing if the appropriate&lt;br&gt;
steps are taken.  &lt;br&gt;
&amp;gt; Besides these two 'fliplr' operations, one 'diff' and one&lt;br&gt;
'find' operation can &lt;br&gt;
&amp;gt; also be eliminated.  Consequently you may find that,&lt;br&gt;
hopefully, a little &lt;br&gt;
&amp;gt; execution time will be saved.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Again, if x is the m by n array whose rows are to be&lt;br&gt;
processed in the &lt;br&gt;
&amp;gt; specified manner, then do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  m = size(x,1);&lt;br&gt;
&amp;gt;  y = [reshape([zeros(1,m);x.'],[],1);0];&lt;br&gt;
&amp;gt;  z = y;&lt;br&gt;
&amp;gt;  p = find(~y);&lt;br&gt;
&amp;gt;  d = 1-diff(p);&lt;br&gt;
&amp;gt;  y(p) = [0;d];&lt;br&gt;
&amp;gt;  y = reshape(cumsum(y(1:end-1)),[],m).';&lt;br&gt;
&amp;gt;  y(:,1) = [];&lt;br&gt;
&amp;gt;  z(p) = [d;0];&lt;br&gt;
&amp;gt;  z = reshape(cumsum(-z(1:end-1)),[],m).';&lt;br&gt;
&amp;gt;  z(:,end) = [];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The y array will then be the above-mentioned 'desired2'&lt;br&gt;
array, and z will be &lt;br&gt;
&amp;gt; the 'desired1' array, both of size m by n.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Yup that works:&lt;br&gt;
Times for processing the whole matrix:&lt;br&gt;
&lt;br&gt;
for-loop (with pre-allocation): ~50 seconds do both&lt;br&gt;
vectorized with fliplr: ~15 seconds&lt;br&gt;
new vectorized: ~10 seconds &lt;br&gt;
&lt;br&gt;
A 5-second improvement is still significant since this will&lt;br&gt;
be calculated thousands of times.&lt;br&gt;
&lt;br&gt;
Since the input matrix is 5188x9110 and it is ~50-60% full&lt;br&gt;
which makes the temp array p is ~27,100,000 of doubles.&lt;br&gt;
&lt;br&gt;
The 'reshape' statement is the only bottleneck.  I separated&lt;br&gt;
out the cumsum statement just to check which was the&lt;br&gt;
offending statement.&lt;br&gt;
&lt;br&gt;
I've been checking the 'bsxfun' since it might let me avoid&lt;br&gt;
the temp arrays.&lt;br&gt;
&lt;br&gt;
On a similar note I've noticed drastically different&lt;br&gt;
performance based on whether the function is implemented as&lt;br&gt;
a stand-alone m-file as opposed to a sub-function.&lt;br&gt;
&lt;br&gt;
Stand-alone times:&lt;br&gt;
for-loop (with pre-allocation): ~50 seconds do both&lt;br&gt;
vectorized with fliplr: ~15 seconds&lt;br&gt;
new vectorized: ~10 seconds &lt;br&gt;
&lt;br&gt;
subfunction times:&lt;br&gt;
for-loop (with pre-allocation): ~55 seconds do both&lt;br&gt;
vectorized with fliplr: ~120 seconds&lt;br&gt;
new vectorized: ~150 seconds &lt;br&gt;
&lt;br&gt;
The choice of implementation is obvious but I'm not sure of&lt;br&gt;
the reason.  I would have expected the opposite results.  If&lt;br&gt;
you have any idea why this would happen it would help&lt;br&gt;
satisfy my intellectual curiosity.&lt;br&gt;
&lt;br&gt;
Thanks&lt;br&gt;
Will&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 13 Dec 2007 06:53:44 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#406001</link>
      <author>William Dampier</author>
      <description>&amp;gt;   William, it has occurred to me that the 'fliplr'&lt;br&gt;
operations are not really &lt;br&gt;
&amp;gt; needed at all in the above processing if the appropriate&lt;br&gt;
steps are taken.  &lt;br&gt;
&amp;gt; Besides these two 'fliplr' operations, one 'diff' and one&lt;br&gt;
'find' operation can &lt;br&gt;
&amp;gt; also be eliminated.  Consequently you may find that,&lt;br&gt;
hopefully, a little &lt;br&gt;
&amp;gt; execution time will be saved.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Again, if x is the m by n array whose rows are to be&lt;br&gt;
processed in the &lt;br&gt;
&amp;gt; specified manner, then do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  m = size(x,1);&lt;br&gt;
&amp;gt;  y = [reshape([zeros(1,m);x.'],[],1);0];&lt;br&gt;
&amp;gt;  z = y;&lt;br&gt;
&amp;gt;  p = find(~y);&lt;br&gt;
&amp;gt;  d = 1-diff(p);&lt;br&gt;
&amp;gt;  y(p) = [0;d];&lt;br&gt;
&amp;gt;  y = reshape(cumsum(y(1:end-1)),[],m).';&lt;br&gt;
&amp;gt;  y(:,1) = [];&lt;br&gt;
&amp;gt;  z(p) = [d;0];&lt;br&gt;
&amp;gt;  z = reshape(cumsum(-z(1:end-1)),[],m).';&lt;br&gt;
&amp;gt;  z(:,end) = [];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The y array will then be the above-mentioned 'desired2'&lt;br&gt;
array, and z will be &lt;br&gt;
&amp;gt; the 'desired1' array, both of size m by n.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Yup that works:&lt;br&gt;
Times for processing the whole matrix:&lt;br&gt;
&lt;br&gt;
for-loop (with pre-allocation): ~50 seconds do both&lt;br&gt;
vectorized with fliplr: ~15 seconds&lt;br&gt;
new vectorized: ~10 seconds &lt;br&gt;
&lt;br&gt;
A 5-second improvement is still significant since this will&lt;br&gt;
be calculated thousands of times.&lt;br&gt;
&lt;br&gt;
Since the input matrix is 5188x9110 and it is ~50-60% full&lt;br&gt;
which makes the temp array p is ~27,100,000 of doubles.&lt;br&gt;
&lt;br&gt;
The 'reshape' statement is the only bottleneck.  I separated&lt;br&gt;
out the cumsum statement just to check which was the&lt;br&gt;
offending statement.&lt;br&gt;
&lt;br&gt;
I've been checking the 'bsxfun' since it might let me avoid&lt;br&gt;
the temp arrays.&lt;br&gt;
&lt;br&gt;
On a similar note I've noticed drastically different&lt;br&gt;
performance based on whether the function is implemented as&lt;br&gt;
a stand-alone m-file as opposed to a sub-function.&lt;br&gt;
&lt;br&gt;
Stand-alone times:&lt;br&gt;
for-loop (with pre-allocation): ~50 seconds do both&lt;br&gt;
vectorized with fliplr: ~15 seconds&lt;br&gt;
new vectorized: ~10 seconds &lt;br&gt;
&lt;br&gt;
subfunction times:&lt;br&gt;
for-loop (with pre-allocation): ~55 seconds do both&lt;br&gt;
vectorized with fliplr: ~120 seconds&lt;br&gt;
new vectorized: ~150 seconds &lt;br&gt;
&lt;br&gt;
The choice of implementation is obvious but I'm not sure of&lt;br&gt;
the reason.  I would have expected the opposite results.  If&lt;br&gt;
you have any idea why this would happen it would help&lt;br&gt;
satisfy my intellectual curiosity.&lt;br&gt;
&lt;br&gt;
Thanks&lt;br&gt;
Will&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 13 Dec 2007 07:09:54 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#406003</link>
      <author>Bruno Luong</author>
      <description>"William Dampier" &amp;lt;walldo2@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fjqkto$9mc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; subfunction times:&lt;br&gt;
&amp;gt; for-loop (with pre-allocation): ~55 seconds do both&lt;br&gt;
&amp;gt; vectorized with fliplr: ~120 seconds&lt;br&gt;
&amp;gt; new vectorized: ~150 seconds &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The choice of implementation is obvious but I'm not sure of&lt;br&gt;
&amp;gt; the reason.  I would have expected the opposite results.  If&lt;br&gt;
&amp;gt; you have any idea why this would happen it would help&lt;br&gt;
&amp;gt; satisfy my intellectual curiosity.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
There is a recent thread that reports slowness of nested&lt;br&gt;
function when handling non-persistence local variable. This&lt;br&gt;
seems to be related to your finding.&lt;br&gt;
&lt;br&gt;
Bruno&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 13 Dec 2007 09:53:18 -0500</pubDate>
      <title>Re: finding consecutive numbers (runs)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813#406024</link>
      <author>Roger Stafford</author>
      <description>"William Dampier" &amp;lt;walldo2@gmail.com&amp;gt; wrote in message &amp;lt;fjqkto$9mc&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; ........&lt;br&gt;
&amp;gt; The 'reshape' statement is the only bottleneck.  I separated&lt;br&gt;
&amp;gt; out the cumsum statement just to check which was the&lt;br&gt;
&amp;gt; offending statement.&lt;br&gt;
&amp;gt; .......&lt;br&gt;
------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Will, I am surprised that you found the three 'reshape' instructions to be &lt;br&gt;
significant bottlenecks.  As I understand it, this instruction by itself does not &lt;br&gt;
require any relative location shifts or alterations in memory, but rather an &lt;br&gt;
altered element-indexing scheme that ought to be comparatively rapid.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;In the case of the first 'reshape', I would think that computing the array, &lt;br&gt;
[zeros(1,m);x.'], would have been much the greater computational task.  This &lt;br&gt;
requires first, a transpose of x involving as it does a complete rearrangement &lt;br&gt;
in memory, and then followed by an expansion of it by inserting m zeros at &lt;br&gt;
equally-spaced intervals throughout its length which means further extensive &lt;br&gt;
displacements.  Perhaps I ought to have written the equivalent expression, &lt;br&gt;
[zeros(m,1),x].', instead, which would allow inserting the m zeros in one &lt;br&gt;
contiguous initial block in memory and then transposing it all afterwards.  &lt;br&gt;
(You might try that.)  Of course, I am only guessing as to the coding tricks &lt;br&gt;
used by MathWorks' programmers.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;For the second two reshapes it seems to me the final transpose in each case &lt;br&gt;
would cause the greatest delay, again because of the necessary &lt;br&gt;
rearrangement in relative memory locations.  The cumsums in each do not &lt;br&gt;
involve rearrangement but simply a cumulative serial addition of successive &lt;br&gt;
elements, which ought to be relatively fast with no fancy indexing schemes &lt;br&gt;
involved.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I am afraid I cannot claim to be an expert on matlab's underlying array &lt;br&gt;
manipulation algorithms, however, since my own matlab system is seriously &lt;br&gt;
outdated (v4a - 1994.)&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
