How to delete previous values within a certain difference?

조회 수: 3 (최근 30일)
Nao
Nao 2022년 10월 27일
댓글: Nao 2022년 10월 28일
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
  댓글 수: 1
Nao
Nao 2022년 10월 28일
oh wow this is so much more simple thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (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