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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by