Find set of all adjacent points that are close to each other

8 ビュー (過去 30 日間)
Paul Hoffrichter
Paul Hoffrichter 2020 年 6 月 5 日
編集済み: Paul Hoffrichter 2020 年 6 月 6 日
I would like to find in Y[n] a set of adjacent points that are close to each other, but I don't get all the desired points. For example.
Y = [ 1.1 1.12 9.2 8.3 8.295 8.292 4.1 4.12 4.19];
dY_idx = find(abs(diff(Y)) < 0.022);
Y_adj_close = Y(dY_idx)
This code results in:
Y_adj_close =
1.1000 8.3000 8.2950 4.1000
But I would like the result to be:
Y_adj_close =
1.1000 1.1200 8.3000 8.2950 8.2920 4.1000 4.12
How can I get the desired result? (Note: I probably could also do a Y = fliplr(Y) followed by a second round and then combine results to get all the unique points, but that would be two sets of operations on a large vector. Hoping for a faster solution.)

採用された回答

Tommy
Tommy 2020 年 6 月 5 日
How about this?
logidx = abs(diff(Y)) < 0.022;
Y_adj_close = Y([logidx 0] | [0 logidx]);
Y_adj_close =
1.1000 1.1200 8.3000 8.2950 8.2920 4.1000 4.1200
  3 件のコメント
Tommy
Tommy 2020 年 6 月 5 日
You're very welcome, happy to help!
Paul Hoffrichter
Paul Hoffrichter 2020 年 6 月 6 日
編集済み: Paul Hoffrichter 2020 年 6 月 6 日
My new question deals with my need to keep dimension of x and y the same. I forgot to mention that in this question. I used your answer here to work out an answer that works. Just wondering whether it can be modified to perform better.
Just using plot in the question to ensure dimensions match, and provide a quick sanity check.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDiscrete Math についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by