MATLAB >> Consecutive Numbers

by Nichola » Wed, 13 May 2009 23:56:01 GMT

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

by Matt Fig » Thu, 14 May 2009 00:11:01 GMT


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

by Nichola » Thu, 14 May 2009 00:23:02 GMT


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

MATLAB >> Consecutive Numbers

by Matt Fig » Thu, 14 May 2009 00:31:01 GMT


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

MATLAB >> Consecutive Numbers

by kannadaraj1 » Thu, 11 Jun 2009 03:37:40 GMT

Thanks very much

MATLAB >> Consecutive Numbers

by Siyi » Thu, 11 Jun 2009 04:36:48 GMT


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

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

MATLAB >> Consecutive Numbers

by Bruno Luong » Thu, 11 Jun 2009 14:45:02 GMT

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

by Sprinceana » Thu, 11 Jun 2009 15:51:02 GMT



MATLAB >> Consecutive Numbers

by PopV Visitkul » Thu, 09 Jul 2009 18:40:17 GMT

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

Similar Threads

1. consecutive numbers of data group(s) - MATLAB

2. Array of consecutive numbers with strings ?

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

3. finding consecutive numbers (runs) - MATLAB

4. Reading a formatted file: problem with consecutive numbers not separated by space

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

5. Consecutive Numbers ! - MATLAB

6. Generate consecutive binary numbers

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

7. consecutive series of numbers - MATLAB

8. what's the best way to find the number of consecutive blocks in

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;
(Please test this before using!)

Good luck, Jan