Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: consecutive numbers
Replies: 8   Last Post: Jul 6, 2013 11:01 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,137
Registered: 6/7/07
Re: consecutive numbers
Posted: Jul 6, 2013 11:01 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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));

--





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.