How to delete previous values within a certain difference?

3 次查看(过去 30 天)
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!

采纳的回答

Davide Masiello
Davide Masiello 2022-10-27
编辑:Davide Masiello 2022-10-27
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by