# How to find the first number, ignore subsequent until a greater number repeats.

32 views (last 30 days)
Mirthand on 7 Apr 2021
Commented: Bruno Luong on 8 Apr 2021
A = [ 0 3 0 3 0 3 0 3 0 4 0 4 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3]
b = find(A==3)
2 4 6 8 28 30 32
Desired Output:
2 28
My attempt is below:
c = diff(b)
d = unique(c)
Which then gives 2 20 but not sure how to go back to the index since this is the diff.
dpb on 7 Apr 2021
Both solutions so far for the hypothesized slightly different input vector.
A = [ 0 3 0 3 0 3 0 3 0 4 0 3 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3]
return
[2 12]
The Q? raised by S Cobeldick seems pertinent if the correct answer is, indeed, to be the one with 28 and not 12 unless it is able to be assured the input pattern must follow that of the first example precisely in not having one of the magic numbers possibly being repeated after the intervening value.

Bruno Luong on 7 Apr 2021
A = [ 0 3 0 3 0 3 0 3 0 4 0 4 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3]:
b = find(A==3);
c = diff(b);
b([1 find(c>c(1),1,'first')+1])
you'll get
ans =
2 28
##### 2 CommentsShowHide 1 older comment
Bruno Luong on 8 Apr 2021
b is the indices in A of 3s
c is the distance between 2 indices,
so
j = find(c>c(1),1,'first')
returns in jthe place where the distance beween 2 indices is larger than the first distance c(1) ("a greater number").
Finally
b([1 j+1])
is just for purpose of getting back indices in A of the 3 and what you call "a greater number repeat"

Matt J on 7 Apr 2021
Edited: Matt J on 7 Apr 2021
Is this what you want?
A = [ 0 3 0 3 0 3 0 3 0 4 0 4 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3];
cA=cummax(A);
b1=find(A==3,1);
b2=find(A==3 & cA>3 ,1);
b=[b1,b2]
b = 1×2
2 28