dpb
Posts:
8,260
Registered:
6/7/07


Re: consecutive numbers
Posted:
Jul 6, 2013 11:01 PM


On 7/6/2013 8:29 PM, fmri wrote: ...
>> > >> x= [1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]; >> > >> s=splitvec(d,'value'); % Bruno's SplitVec @ FEX >> ... ... >> 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{:}] ...
> 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 ...
I'm still learning them meself having only had a release that contains them <1 yr and not actively doing any technical work (my day job is farming now). So, when I thought about the length vectors as a way I took it as a challenge to use it... :)
BTW, it would be a little cleaner to get the final result above as
res=cell2mat(arrayfun(@(x,y) (repmat(y,1,x)), cnts,(1:length(cnts)), ... 'uniformoutput',false));


