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

consecutively repeated duplicates

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?

Best Answer

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