How to identify and remove element if it is consecutively repeated a certain amount of times in an array
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shuo Zhang
le 7 Août 2019
Réponse apportée : Alex Mcaulley
le 7 Août 2019
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?
Réponse acceptée
Alex Mcaulley
le 7 Août 2019
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!