If i have the following array
a =[1,1,1,2,2,1,1,6,6,6,8,8,2,2,3,3,2,2]
When i do unique(a,'stable'), i get
[1,2,6,8,3]
I want the following result
[1,2,1,6,8,2,3,2]

 채택된 답변

Stephan
Stephan 2019년 3월 3일

1 개 추천

b = a(logical([1 (diff(a)~=0)]))

댓글 수: 2

Esegboria Osarhemen
Esegboria Osarhemen 2019년 3월 3일
Thanks
(diff(a)~=0) is a logical vector. Concatenating it with a double converts it to a double. Casting it by logical() converts it again. More efficient:
b = a([true, diff(a)~=0])
Now the values are not converted twice.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brent F
Brent F 2021년 6월 10일
편집: Brent F 2021년 6월 10일

0 개 추천

Using @Jan's idea, but:
  • Package as a function
  • Handle case where column-vector is given (return in same format as given)
function [uniqueSequence] = DedupSequence (seq)
% Eliminate sequentially repeated rows
% Create row vector for diff (must transpose if given a column vector)
if size(seq,1) > 1
seqCopy = seq(:,1)';
else
seqCopy = seq;
end
uniqueSequence = seq([true, diff(seqCopy)~=0]);
end

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2019년 3월 3일

편집:

2021년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by