Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: consecutive numbers
Date: Sun, 7 Jul 2013 01:29:10 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <krag96$69f$1@newscl01ah.mathworks.com>
References: <kr6gus$b8d$1@newscl01ah.mathworks.com> <kr9t63$mk1$1@speranza.aioe.org> <kra0lq$vhu$1@speranza.aioe.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1373160550 6447 172.30.248.38 (7 Jul 2013 01:29:10 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 7 Jul 2013 01:29:10 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 4279012
Xref: news.mathworks.com comp.soft-sys.matlab:798931

dpb <none@non.net> wrote in message <kra0lq$vhu$1@speranza.aioe.org>...
> On 7/6/2013 3:03 PM, dpb wrote:
> > On 7/5/2013 8:16 AM, fmri wrote:
> > ...
> >
> >> example :
> >> x= [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]
> ...
> >> ... I want:
> >> y= [ 1 1 1 2 2 2 2 3 3 4 4 4 4 4]
> > ...
> >
> >  >> x= [1 2 3 7 8 9 10 13 14 20 21 22 23 24 ];
> >  >> s=splitvec(d,'value'); % Bruno's SplitVec @ FEX
> ...
> 
> > I like the cnts logic! :)...
> 
> It dawned on me just as I walked away that the above logic isn't 
> general; it works for the specific case because there's no consecutive 
> values that aren't separated by other than one--a sequence of any fixed 
> delta would be counted just looking at length of split cells.
> 
> OTOH the following should work for any vector for consecutive values...
> 
>  >> cnts=cellfun(@length,s(cellfun(@(x) all(x==1),s)))+1;
>  >> res=arrayfun(@(x,y) (repmat(y,1,x)), cnts,(1:length(cnts))  ...
>                                 ,'uniformoutput',false);
>  >> res=[res{:}]
> res =
>    1  1  1  2  2  2  2  3  3  4  4  4  4  4
>  >>
> 
> The above finds all cells in the split vector in which all values are 
> 1's; those are the consecutive values.  The length of the difference 
> vector is always one less than the number, hence the +1.  One more 
> practice at writing anon function and arrayfun (I have only recently had 
> a release of Matlab that includes these so I'm still pretty much a 
> novice w/ them....couldn't get nested repmat and horzcat to play well to 
> gether as arrayfun intervened first always complaining about nonuniform 
> output before horzcat got a chance't at it...not sure about that one at 
> the moment as far as a way to avoid the intermediary.  The loop may be 
> as good if just preallocate--you can determine the necessary lengths 
> easily enough.
> 
> --

Hi dpb, 

thanks for this. Honestly, I was facing some problems while applying yr commands. I am still learning the basic functions and always get confused when I see cellfun and arrayfun ...