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

2 Ansichten (letzte 30 Tage)
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.
  5 Kommentare
Mirthand
Mirthand am 7 Apr. 2021
Where in your code do you check the "until a greater number repeats" requirement?
That's true, I think it doesn't nececessarily need to follow that.
dpb
dpb am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 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 Kommentare
Bruno Luong
Bruno Luong am 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"

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 7 Apr. 2021
Bearbeitet: Matt J am 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

Community Treasure Hunt

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

Start Hunting!

Translated by