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

2 visualizaciones (últimos 30 días)
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 comentarios
Mirthand
Mirthand el 7 de Abr. de 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 el 7 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 7 de Abr. de 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 comentarios
Mirthand
Mirthand el 8 de Abr. de 2021
Can you explain this line?
b([1 find(c>c(1),1,'first')+1])
Bruno Luong
Bruno Luong el 8 de Abr. de 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"

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 7 de Abr. de 2021
Editada: Matt J el 7 de Abr. de 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