function Y = count(X) %COUNT Frequency of unique rows. % Y = COUNT(X) computes the frequency of each unique row of an M-by-N matrix % X and returns an M-by-(N+1) matrix Y sorted in ascending order of % frequency, whose first column contains the count for the corresponding % row of X. % % Example: % >> count([10 20 30; 40 50 60; 10 20 30]) % % ans = % % 1 40 50 60 % 2 10 20 30 [b,i,j] = unique(X,'rows'); numUniq = size(b,1); Y = [zeros(numUniq,1), b]; for k = 1:numUniq Y(k,1) = sum(j==k); end Y = sortrows(Y);