Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Divide consecutive numbers in matrix

Subject: Divide consecutive numbers in matrix

From: EJ

Date: 1 Nov, 2012 15:16:07

Message: 1 of 3

I have matrix, of which elements are a=[2,3,4,7,9,12,13,14,15]
In such a case, how can I get the below values?

ans = 2,3,4
ans = 7
ans = 9
ans = 12,13,14,15

I would like to get the separate values that are consecutive numbers.
Thanks

Subject: Divide consecutive numbers in matrix

From: Don Ron

Date: 1 Nov, 2012 16:32:08

Message: 2 of 3

"EJ" wrote in message <k6u3nn$m9j$1@newscl01ah.mathworks.com>...
> I have matrix, of which elements are a=[2,3,4,7,9,12,13,14,15]
> In such a case, how can I get the below values?
>
> ans = 2,3,4
> ans = 7
> ans = 9
> ans = 12,13,14,15
>
> I would like to get the separate values that are consecutive numbers.
> Thanks

[~,c]=find(diff(a)>1);
b{1}=a(1:c(1));
for i1=2:length(c)
b{i1}=a(c(i1-1)+1:c(i1));
end
b{i1+1}=a(c(end)+1:end);

don.

Subject: Divide consecutive numbers in matrix

From: Bruno Luong

Date: 1 Nov, 2012 21:56:08

Message: 3 of 3

You can use this FEX:

http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter

>> a=[2,3,4,7,9,12,13,14,15]

a =

     2 3 4 7 9 12 13 14 15

>> c=SplitVec(a,'cons');
>> c{:}

ans =

     2 3 4


ans =

     7


ans =

     9


ans =

    12 13 14 15

% Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us