Vector counting syntax help?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
James Baker
am 9 Mai 2021
Kommentiert: Star Strider
am 9 Mai 2021
if i have a vector A = [1 1 1 1 2 3 3 5 5 5 5 6 6 7]... its alreay been sortecand only increases.. i want to create a 2nd vector based off A say called B that would have the count of each unique values in the A vector... so, B = [3 1 2 3 2 1] .. because there are 3 1's and 1 2 and 2 3's, etc etc. i just cant seem to get the suntax correct... please help.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 9 Mai 2021
Try this —
A = [1 1 1 1 2 3 3 5 5 5 5 6 6 7];
[Au,~,ix] = unique(A,'stable');
Count = accumarray(ix,1);
Result = table(Au(:), Count(:), 'VariableNames',{'Unique_Elements','Count'})
.
4 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!