For example:
1, 2, 3, 4, 5, 10, 11, 13
Should be divided into:
1:5
10:11
13
I've written a function that goes over the values and compares the number before and after a certain element, but I'm quite sure there's a faster and better way.
Thanks a lot.
D = ~~([1 diff(x - (1:length(x)))]) % Where are the ones?
Thanks. I see your point, but could you please explain the ~~ for me? Thanks.
This makes D a logical vector. Actually this is not strictly necessary, depending on how you would proceed from here. I might do something like this:
x = [1 2 3 4 5 10 11 13 14]
F = diff(find([1 diff(x - (1:length(x)))]));
mat2cell(x,1,[F length(x)-sum(F)])
Essentially the same method as Matt's but a oneliner:
mat2cell(x,1,diff([0,find(diff(x) ~= 1),length(x)]))
>> a=[1, 2, 3, 4, 5, 10, 11, 13]
a =
1 2 3 4 5 10 11 13
>> SplitVec(a,'consecutive')
ans =
[1x5 double] [1x2 double] [13]
% Bruno
Would it be possible to explain the principle behind the code by any chance please? What I really like about it is that it managed to group the consecutive numbers really nicely in a large file. What I am trying to do now is grouping bunches of zeros and ones from a random mixture of zeros and ones sequence. I slightly modified your code for do it yesterday. However it seems to only group the the zeros together while leaving all the ones ungrouped. This is because i didn't know what i was doing. Your explanation would be highly appreciated.
Many thanks,
Viput
"Matt Fig" <spam...@yahoo.com> wrote in message <guesk5$19s$1...@fred.mathworks.com>...