Here are all the ways in which you could count unique elements:
M = randi([1 7], [1500 1]);
Option 1: table
t = tabulate(M); counts1 = t(t(:,2)~=0, 2);
Option 2: hist / histc
counts2_1 = hist( M, numel(unique(M)) ); counts2_2 = histc( M, unique(M) );
Option 3: Drive
counts3 = accumarray(M, ones(size(M)), [], @sum); %
Option 4: sort / diff
[MM idx] = unique( sort(M) ); counts4 = diff([0;idx]);
Option 5: arrayfun
counts5 = arrayfun( @(x)sum(M==x), unique(M) );
Option 6: bsxfun
counts6 = sum( bsxfun(@eq, M, unique(M)') )';
Option 7: sparse
counts7 = full(sparse(M,1,1));
Amro May 21 '10 at 19:59 2010-05-21 19:59
source share