Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: finding consecutive numbers (runs)
Date: Thu, 13 Dec 2007 05:17:55 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 44
Message-ID: <fjqfa3$ce4$1@fred.mathworks.com>
References: <fjp9pj$p8g$1@fred.mathworks.com> <fjpihi$p2j$1@fred.mathworks.com> <fjplk2$fgm$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1197523075 12740 172.30.248.38 (13 Dec 2007 05:17:55 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 13 Dec 2007 05:17:55 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:442268


"William Dampier" <walldo2@gmail.com> wrote in message <fjplk2$fgm
$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
> wrote in message <fjpihi$p2j$1@fred.mathworks.com>...
> > .......
> >  [m,n] = size(x);
> >  y = [zeros(1,m);x.'];
> >  y = y(:);
> >  p = find(~y);
> >  y(p) = [0;1-diff(p)];
> >  y = reshape(cumsum(y),[],m).';
> >  y(:,1) = [];
> 
> Thanks a whole lot.
> It took me a few minutes to realize that if I fliplr(input)
> then I'll also have to fliplr the output as well. like this:
> .......
--------
  William, it has occurred to me that the 'fliplr' operations are not really 
needed at all in the above processing if the appropriate steps are taken.  
Besides these two 'fliplr' operations, one 'diff' and one 'find' operation can 
also be eliminated.  Consequently you may find that, hopefully, a little 
execution time will be saved.

  Again, if x is the m by n array whose rows are to be processed in the 
specified manner, then do this:

 m = size(x,1);
 y = [reshape([zeros(1,m);x.'],[],1);0];
 z = y;
 p = find(~y);
 d = 1-diff(p);
 y(p) = [0;d];
 y = reshape(cumsum(y(1:end-1)),[],m).';
 y(:,1) = [];
 z(p) = [d;0];
 z = reshape(cumsum(-z(1:end-1)),[],m).';
 z(:,end) = [];

The y array will then be the above-mentioned 'desired2' array, and z will be 
the 'desired1' array, both of size m by n.

Roger Stafford