MATLAB: Count number of times value occurs

frequencyhistogram

Hi,
I have an array a = [ 1 2 3 2 2 3 1], and another array, b = [1:5].
How can I create an array c, that counts the number of times that each value in b occurs in a?
I want c = [2 3 2 0 0] (ie. 1/2/3/4/5 from b, appears 2/3/2/0/0 times in a)
Any help would be appreciated. Thanks,
DB

Best Answer

Method for large a and b
[u,~,j] = unique(b(:));
[b, i] = ismember(a(:),u);
c = accumarray(i(b),1,size(u));
c = c(j)'