# How to find the first element in a vector of differing consecutive values?

72 views (last 30 days)
Russell Evans on 22 May 2018
Edited: Jan on 24 May 2018
If I have a vector [636 637 638 24639 24640 48640 48641 48642 48643], what code can I write to pull the first element in each consecutive series of elements within the vector. In the above example, the elements I would want are [636 24639 48640]. The elements are not always the first, fourth, and sixth indices. But rather, I can produce a vector that contains elements that can be consecutive at times, yet I only want the first element in the sequence of consecutive elements.
Thank you

Fangjun Jiang on 22 May 2018
Something like this?
a=[636 637 638 24639 24640 48640 48641 48642 48643];
b=[0 a];
c=diff(b);
index=c>1;
out=a(index);
Jan on 23 May 2018
The author will ask by himself, if he needs modifications. It is his thread. Speculations, that he wants beside the shown growing sequences of positive numbers also shrinking sequences of negative numbers is confusing only, because there is no evidence for this. Why not growing sequences of negative numbers, or complex values, or steps apart from 1.0, or quaternions, or characters insensitive for case?
Of course the editors do not get access to the contact information of other members. And even if they do, they would not be so impolite to push authors of questions personally.
Together with Guillaume's addition, Fangjun's solution is neat and powerful:
out = a(diff([0 a]) ~= 1)
Or to catch shrinking sequences also:
out = a(abs(diff([0 a])) ~= 1)
Logical indexing is nicer and more efficient than a find.

John BG on 22 May 2018
Edited: John BG on 22 May 2018
Hi Russel
this is John BG jgb2012@sky.com
1.- generating data
N=5
a=randi([-1000 1000],1,N);
b=2*(randi([0 1],1,N)-.5);
c=randi([5 15],1,N);
L=[]
for k=1:1:N
L=[L repmat(a(k),1,c(k))+[0:b(k):b(k)*c(k)-b(k)]];
end
2.-
Let be for instance
L
L =
Columns 1 through 10
-492 -493 -494 -495 -496 -497 -498 -499 -500 -501
Columns 11 through 20
-502 -503 -504 -505 629 628 627 626 625 624
Columns 21 through 30
623 622 621 620 619 -513 -512 -511 -510 -509
Columns 31 through 40
-508 -507 -506 -505 -504 -503 859 858 857 856
Columns 41 through 50
855 854 853 852 851 850 849 848 847 846
Columns 51 through 59
845 -300 -301 -302 -303 -304 -305 -306 -307
3.-
L2=abs(abs(diff(L))-1);
S=[L(1) L(find(L2>0)+1)]
S =
-492 629 -513 859 -300
a=randi([-1000 1000],1,N);
the input sequence should positive only, then replace with
a=randi([1 1000],1,N);
It also works with positive integers input only.
.
Comment: these 2 simple lines work for positive or negative integers, and for ascending +1 or descending -1 bursts.
.
Russel
thanks in advance for time and attention
John BG
Paolo on 23 May 2018
John,
The reason I removed my solution:
A = [636 -637 -638 24639 24640 48640 48641 48642 -48643];
out=[A(1) A(find((diff(A)~=1)&(diff(A)~=-1)&(diff(A)~=0))+1)];
is because it does not provide a complete solution to the problem. As you correctly pointed out, it does not deal successfully with variations that differ from +1 and -1. I agree with you that the solution should successfully deal with any sequence, as you pointed out in the comment to Fangjun's answer. However, none of the solutions provided so far can do that.