Azzera filtri
Azzera filtri

How to delete previous values within a certain difference?

2 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Davide Masiello
Davide Masiello il 27 Ott 2022
Modificato: Davide Masiello il 27 Ott 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by