How can I find equal elements in a vector and place them at the end of the vector?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
giancarlo maldonado cardenas
am 15 Dez. 2021
Beantwortet: Chunru
am 15 Dez. 2021
Hello, how can I find equal elements within a vector, and once I find these repeated elements, place them at the end? for example I have a vector.
vector = [10 22 22 22 30 45 45 20]
the result of the new vector would have to be
newvector = [10 30 20 22 22 22 45 45]
thanks in advance
0 Kommentare
Akzeptierte Antwort
Chunru
am 15 Dez. 2021
vector = [10 22 22 22 30 45 45 20]';
[GC,GR] = groupcounts(vector);
[GC, idx] = sort(GC);
GR=GR(idx);
newvec = [];
for i=1:length(GR)
newvec =[newvec; vector(vector==GR(i))];
end
newvec
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!