How to identify and remove element if it is consecutively repeated a certain amount of times in an array

조회 수: 1 (최근 30일)
Hi,
I'd like to remove duplicates in an array only when it is consecutively repeated for a certain amount of time.
For example, say I have an array A=[1 2 3 3 3 3 5 6 2 8 7 3 3 2]; I'd like to identify and remove any element that is consequtively repeated for 3 times and more.
Specifically, I mean to identify and remove the 4 threes at A(3),A(4),A(5),A(6); While for the 2 threes at A(12), A(13), and the 3 twos at A(2),A(9),A(14), I'd like to keep them because either they are repeated but not consequtively, or not for 3 times and more.
I've searched how to remove duplicates, but it doesn't seem very helpful.
Is there any good solution to it?

채택된 답변

Alex Mcaulley
Alex Mcaulley 2019년 8월 7일
Following Jan's answer in this threat (link):
A = [1 2 3 3 3 3 5 6 2 8 7 3 3 2];
d = [true, diff(A) ~= 0, true];
n = diff(find(d));
Y = repelem(n, n)
A = A(Y < 3)
A =
1 2 5 6 2 8 7 3 3 2

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by