How to delete adjacent values

조회 수: 7 (최근 30일)
Nao
Nao 2022년 10월 25일
댓글: Nao 2022년 10월 27일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by