MATLAB: Count the number of same elements in an array

arrayarrayscode generationelementsfindMATLABrepeated

Hi given a vector
V = [ 1 2 4 3 4 2 3 5 6 4 5 6 8 4 2 3 5 7 8 5 3 1 3 5 7 8 9 5 3 2 4 6 7 8]
I would like to count how many times the value 1,2,3,4,5,6,7,8,9 are repeated inside V, and obtain a vector that report this values:
C = [2 4 6 5 6 3 3 4 1]
where 1 is repeated 2 times, 2 is repetead 4 times, 3 is repeated 6 times and so on..

Best Answer

[~,~,ix] = unique(V);
C = accumarray(ix,1).'