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 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by