How to delete adjacent values

Hello
I have a vector of 1400x1 with several adjacent values (values with a difference of less than 1).
For example, my data looks like
A = [1 5 6 7 8 9 10 11 12 20 21 22 23]'
I'd like to delete the row that is adjacent to the previous one, so my expected outcome will be [1; 5; 20]
Any ideas on how to do this? I appreciate your help!

 採用された回答

Davide Masiello
Davide Masiello 2022 年 10 月 25 日

0 投票

A = [1 5 6 7 8 9 10 11 12 20 21 22 23];
A([false,diff(A) == 1]) = []
A = 1×3
1 5 20

5 件のコメント

Nao
Nao 2022 年 10 月 25 日
thank you so much for your swift response.
I get an error with this saying:
Error using horzcat
Dimensions of matrices being concatenated are not consistent
Probably because the data is lined vertically in rows (its a vector of 1400 x 1). I'm very new at this so apologize for my lack of understanding;(
Davide Masiello
Davide Masiello 2022 年 10 月 25 日
編集済み: Davide Masiello 2022 年 10 月 25 日
Yes, if you have a column vector then you need to write
A([false;diff(A) == 1]) = []
Nao
Nao 2022 年 10 月 25 日
it worked!! thank you so much!!
Image Analyst
Image Analyst 2022 年 10 月 26 日
But since you said "a difference of less than 1" you'd want <1 instead of == 1
A([false;diff(A) < 1]) = []
Of course then your expected outcome is not right. Maybe you mean "a difference of 1 or less" in which case it would be
A([false;diff(A) <= 1]) = []
Nao
Nao 2022 年 10 月 27 日
thats a great point! thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

質問済み:

Nao
2022 年 10 月 25 日

コメント済み:

Nao
2022 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by