MATLAB: Finding rows in a matrix

count

I have a matrix A = [1 2; 2 1; 1 2; 2 2; 1 1; 2 2]
I want to count how many times the row [1 2] appears in above matrix A. Here for my counting purpose [1 2] would appear 3 times as [1 2] or [2 1].
Thanks guys

Best Answer

Another:
% The given matrix
A = [1 2; 2 1; 1 2; 2 2; 1 1; 2 2];
% Now find the counts.
[I,J,K] = unique(sort(A,2),'rows'); % I has the unique rows.
C = histc(K,1:max(K)); % This has the corresponding counts.
% Now that we have found the counts, display them:
fprintf('Row [%i %i] appears %i times. \n',[I C]')
If you want to only get the counts for the one type, this will do it quickly:
cnt = sum(all(bsxfun(@eq,sort(A,2),[1,2]),2));