Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Join repeated continuous elements of a vector

조회 수: 1 (최근 30일)
Alexander
Alexander 2020년 2월 27일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi, is there a simple method to join repeated continuous elements of a vector in Matlab?
For example if the input is:
in = [1 1 1 2 2 3 6 6 6 1 1 1 4 4 4 2 2 2 6 4 4]
how can i get
out = [1 2 6 1 4 2 6 4]
Note that since 3 is not repeted, it is not in 'out' vector. Same for the only 6.
thanks !
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 27일
No, there is no simple method. There are some methods that take a few lines.
Alexander
Alexander 2020년 2월 27일
Thank you !

답변 (1개)

Are Mjaavatten
Are Mjaavatten 2020년 2월 27일
in = [1 1 1 2 2 3 6 6 6 1 1 1 4 4 4 2 2 2 6 4 4];
i = 1;
count = 1;
out = [];
a = in(1);
while i < length(in)
i = i+1;
if in(i) ~= a
count = 1;
a = in(i);
else
count = count + 1;
end
if count == 2;
out = [out,a];
end
end
disp(out)
  댓글 수: 1
Alexander
Alexander 2020년 2월 27일
Thanks for your time
Your code works just fine !
This one works too:
in = [1 1 1 2 2 3 6 6 6 1 1 1 4 4 4 2 2 2 6 4 4];
BB=diff(find(diff(in)~=0));
in(diff(m)==0) = [];
in(find(BB==1)+1)=[];
out = in;

Community Treasure Hunt

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

Start Hunting!

Translated by