Need to remove repeated adjacent elements in an array
이전 댓글 표시
I need to turn
[1 1 1 1 2 2 2 6 6 6 6 2 2 2 2] into [1 2 6 2]
unique() gives [1 2 6], but I want to preserve the second value
any advice?
댓글 수: 3
Michael Cappello
2015년 5월 15일
x(diff(x)==0) = []
Matthew Rademacher
2015년 5월 16일
Ravi Mravi
2017년 10월 30일
Excellent solution
채택된 답변
추가 답변 (2개)
Joseph Cheng
2015년 5월 15일
편집: Joseph Cheng
2015년 5월 15일
you can use diff to determine the consecutive same value numbers
test = [1 1 1 1 2 2 2 6 6 6 6 2 2 2 2]
mtest = [test test(end)-1];
difftest = diff(mtest)
output = test(difftest~=0)
the mtest is the modified test number to get the last value not the same. if you look at the output of difftest you see that we get the positions of the transitions from one number to another.
Image Analyst
2015년 5월 15일
Here's one way:
m = [1 1 1 1 2 2 2 6 6 6 6 2 2 2 2]
logicalIndexes = [0, diff(m)] ~= 0
output = [m(1), m(logicalIndexes)]
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!