MATLAB Answers

Find the first and last elements of consecutive values in a vector

86 views (last 30 days)
Let's say we have an arbitrary vector like this with ascending values:
a= [10 11 12 19 21 25 29 30 31 32 33 39 50];
and we want to find the first and last element of each consecutive (integer) values. The answer could be in this form:
First consecutive values: [10 11 12]
Second consecutive values: [29 30 31 32 33]
CI_f = [10 29]
CI_l = [12 33]
How could we do it in MATLAB without loop?
Is it also possible to put a condition for the length of the consecutive values to be between specific numbers of values, let's say between 3 and 8?
Thanks in advance for helping :)

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 27 Mar 2018

The second sequence seems to have a mistake, given a it should be:

Second consecutive values: [29 30 31 32 33]
CI_f = [10 29]

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 27 Mar 2018
Edited: Stephen Cobeldick on 27 Mar 2018
It is easy to identify the first and last elements of consecutive sequences of values:
>> a = [10 11 12 19 21 25 29 30 31 32 33 39 50];
>> D = diff([0,diff(a)==1,0]);
>> first = a(D>0)
first =
10 29
>> last = a(D<0)
last =
12 33
"Is it also possible to put a condition for the length of the consecutive values to be between specific numbers of values, let's say between 3 and 8?"
Yes. Add find and experiment with the differences:
1 + find(D<0) - find(D>0)
Hint: compare against the require limits to generate an index.

  0 Comments

Sign in to comment.

More Answers (1)

Pawel Jastrzebski
Pawel Jastrzebski on 27 Mar 2018
As to what you put as an answer, it can be coded this way:
CI_f = [CI{1}(1) CI{2}(1)]
CI_l = [CI{1}(end) CI{2}(end)]
And the output is going to be the same:
>> CI_f
CI_f =
10 30
>> CI_l
CI_l =
12 33
However I find it your question completely unclear.
  • You've got a vector 'a' consisting of 13 elements.
  • Values from that vector are used to create the cell arrays
  • 1st cell array CI{1} holds 3 elements which are consecutive vales of 'a': indices 1 to 3
  • 2nd cell array CI{2} holds 4 elements which are consecutive vales of 'a': indices 8 to 11
  • Where's the pattern here?
  • Are there going to be more cell arrays? I.e. CI{3} holding 5 elements of 'a': what indices??
  • What are you trying to achieve?
  • Why CI's are stored as cells? What's wrong with the row vectors?

  4 Comments

Show 1 older comment
Pawel Jastrzebski
Pawel Jastrzebski on 27 Mar 2018

OK, from what I've seen, you've changed the consecutive values from cells to vectors.

Once you've got your consecutive vectors created , it easy to find the first and the last value in them - again:

a = [10 11 12 19 21 25 29 30 31 32 33 39 50];
CI_1 = [10 11 12];
CI_2 = [30 31 32 33];
% First and last value in each CI vector:
CI_f = [CI_1(1) CI_2(1)]
CI_l = [CI_1(end) CI_2(end)]
  • What is the rule that decides that CI_1 will have 3 elements, CI_2 will hold 4 elements etc.?
  • What is the rule that decides that CI_1 1st value = 1st value of a? And then the 1st value of CI_2 = 8th value of a? Are you choosing them randomly?

As to your sub-question:

Is it also possible to put a condition for the length of the consecutive values to be between specific numbers of values, let's say between 3 and 8?

Yes, it is possible.

Test the following code:

a = [10 11 12 19 21 25 29 30 31 32 33 39 50];
% STARTING POINT
% select a random staring point which is within the length of 'a'
StartPoint = randi(length(a))
% LENGTH
% Chose the ranodm lenth
Random_Length = randi([StartPoint length(a)])
CI_3 = a(StartPoint:Random_Length)
RZM
RZM on 27 Mar 2018
Thanks but this is still not what I meant. If we start reading "a" from left to right as we normally do, CI_1 is the first sequence of consecutive numbers. The next sequence of consecutive values in "a" is 29 to 33 so the CI_2 is [29 30 31 32 33]. There is no other rule in creation of "a" despite it has to have ascending integer values with growing indices and a sequence here refers to the consecutive ascending values with the difference of "one".
Pawel Jastrzebski
Pawel Jastrzebski on 27 Mar 2018
OK, now I see what you meant - sorry, I originally missed the point.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by