How to delete previous values within a certain difference?

2 vues (au cours des 30 derniers jours)
Nao
Nao le 27 Oct 2022
Commenté : Nao le 28 Oct 2022
Hello.
I have a vector A (1571x1), and many values are adjacent, for example [1,2,3,4,20,21,22,35,36,37,38...]. I want to delete all the previous values with a difference of less than 10, and keep the biggest value in that continuous series. So my expected outcome will be [4,22,38...].
I have this code:
keep = false(size(A));
b = -Inf;
for i=1:length(A)
if CLIMBDOWN(i) >= b
keep(i) = true;
b = A(i) + 10;
end
end
A = A(keep);
but this deletes the values that comes after, so it keeps the smallest in that series (exp. [1,20,35...]). I have tried changing the 6th row of the code to -10, but the code did not run properly and did not delete any numbers.
Any ideas on how I can modify this? Or perhaps a different code that will do the job?
Thank you for your help!

Réponse acceptée

Davide Masiello
Davide Masiello le 27 Oct 2022
Modifié(e) : Davide Masiello le 27 Oct 2022
You can do this
A = [1,2,3,4,20,21,22,35,36,37,38,50,51,52,53,54,57,70];
A = A(diff(A)>10)
A = 1×4
4 22 38 57
  1 commentaire
Nao
Nao le 28 Oct 2022
oh wow this is so much more simple thank you so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by