### MATLAB >> Consecutive Numbers

Hi. Given a series of sorted numbers, is there a function in MATLAB that recognizes the consecutive values?

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.

### MATLAB >> Consecutive Numbers

Look at:
x = [1 2 3 4 5 10 11 13];

D = ~~([1 diff(x - (1:length(x)))]) % Where are the ones?

### MATLAB >> Consecutive Numbers

Thanks. I see your point, but could you please explain the ~~ for me? Thanks.

### MATLAB >> Consecutive Numbers

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

Thanks very much

### MATLAB >> Consecutive Numbers

Essentially the same method as Matt's but a oneliner:

mat2cell(x,1,diff([0,find(diff(x) ~= 1),length(x)]))

### MATLAB >> Consecutive Numbers

Or using SplitVec on FEX: http://www.mathworks.com/matlabcentral/fileexchange/24255

a =

1 2 3 4 5 10 11 13

ans =

[1x5 double] [1x2 double] [13]

% Bruno

### MATLAB >> Consecutive Numbers

Hi Matt,

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

```Hi All,

Sorry about the horrible subject title for this message.

Say I have the following array..

2	'word1'
5	'word2'
6	'word3'
7	'word4'
10	'word5'
14	'word6'
16	'word7'
17	'word8'
20	'word9'

I want to leave numbers without consecutives as it is, but gorup numbers with consecutives into a cell array

In other words, I want the following result

'word1'
<3x1 cell>        <-- which consists of word2 word3 and word4
'word5'
'word6'
<2x1 cell>        <-- which consists of word7 and word8
'word9'

It will be great if you someone can help out.

Thanks.

Regards,
Simon
```

```Hi all,
I'm trying to read a formatted file.

Each line looks like the following:

20061010  0   0.25130.10  4.05
3.72-99.00-99.00-99.00-99.00-99.00-99.00-99.00  1.76 -2.44

The format of the line, is:
1) a 4 digit integer
2) a 2 digit integer
3) a 2 digit integer
then there is a space
4) a 2 digit integer
then another space
and then, from now on, all floating point numbers having 6digits fields
with 2 digits after the dot.
In FORTRAN:
FORMAT(I4,I2,I2,1X,I2,1X,13F6.2)

As you see, a problem exists because different fields are not always
separated by a space(e.g. in the previous line the number 0.25 is not
spearated from 130.10)

I tried to use the following in matlab:

fscanf(fid,'%4d%2d%2d %2d %6f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f\n',[17
inf])

but if I use the previous command, the 0.25 and the 130.10 re not
separated. Instead, the fields are separated as 0.2513 and 0.10.
I suppose I'm doing something wrong in the fields definition, but I
tried other things (like %6.2f) withouth success...
any idea why is it happening? How should I do?
Bye,
Gab

```

```How can I quickly generate a logical matrix of consecutive binary numbers from 1 to say 2^25? I also don't want to use too much memory. The way I do it now is:

logical(uint8(dec2bin(1:2^numc-1,numc)) - uint8('0'))

Can it be done better?

Thanks
```

```Dear Luna!

> > > I want to find the length of longest consecutive stripe in that
> > > column...

You can find the indices of changes between the values with:
Ind = find(diff(x))
Then you can find the number of elements between the changes with a further DIFF:
Len = diff(Ind)
Now [Num, MaxInd] = MAX(Len) gives the index and number of the longest sequence. *But* the marginal elements are not correctly considered now!
Ind = [0; find(diff(x)); length(x)];
[LongestSeq, MaxInd] = max(diff(Ind));
LongestInd = Ind(MaxInd) + 1;