Sort a n array for maximum number of recurring values and storing the indexes corresponding to erased components

1 view (last 30 days)
Hi,
i'm working with an array of thousands of elements and i've to limit the repeated values to 10.
Let say:
a=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9];
has to become:
[0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,9].
And i want to store the indexes corresponding to the erased 10+-times recurrent values.
Any suggestion would be appreciated.
Thanks in advance,
Alessandro
  2 Comments
Matt J
Matt J on 20 Jan 2021
And i want to store the indexes corresponding to the erased 10+-times recurrent values.
If I have a repitiion of a number, say [2 2 2 2 2] and I reduce it to [2,2,2,2] clearly I have erased one of the 2's. But which one? The first? The last? One of them in the middle?
Alessandro Togni
Alessandro Togni on 20 Jan 2021
Edited: Alessandro Togni on 20 Jan 2021
The 10th, 11th, etc of each list of 10+ occurrences!
Could be useful to say that i have to edit other vector of the same lenght in the same way on the one we're talking about.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 20 Jan 2021
Edited: Matt J on 20 Jan 2021
a=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9];
s=diff([inf,a])~=0;
b=ones(size(a));
b(s)=b(s)-[0,diff(find(s))];
discard=(cumsum(b)>10); %indices to discard
a_short=a(~discard) %truncated version of a
a_short = 1×28
0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 9

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!