フィルターのクリア

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

2 ビュー (過去 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 Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by