Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Matt J
Matt J am 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 am 20 Jan. 2021
Bearbeitet: Alessandro Togni am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 20 Jan. 2021
Bearbeitet: Matt J am 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

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by