MATLAB: How to keep count of occurence of column combination using ‘for’ loop

occurencepair combination

Iam having matrix with large number of rows and column, I want to check the occurence of pair column cobminations (here for eg: I have taken 2 column) and based on that occurence I want to write every multiple of 2 count (mean to say: count 2,4,6… rows ) data into file .
Code I tried is:
Eg: A = [1,1;2,1;3,1;1,2;2,2;3,2;1,3;2,3;3,3;1,1;2,1;3,1;1,2;2,2;3,2;1,3;2,3;3,3;1,1;2,1;3,1;1,2;2,2;3,2;1,3;2,3;3,3;1,1;2,1;3,1]
b = unique(A,'rows'); % b = [1,1; 2,1; 3,1; 1,2; 2,2; 3,2; 1,3; 2,3; 3,3]
for k = 1:size(A(:,1))
count = (sum(A(:,1)==b) & sum(A(:,2)==b) % this is wrong but not able to figure it out how to write the logic.
if mod(count,2)==0
disp(count);
% writing data to file
end
end
May be solution is easy , since iam new to matlab iam not able to figure it out.
Thanks in advance.

Best Answer

How to check the number of occurence of pairs in the nx2 matrix A.
A = [1,1;2,1;3,1;1,2;2,2;3,2;1,3;2,3;3,3;1,1;2,1;3,1;1,2;2,2;3,2;1,3;2,3;3,3;1,1;2,1;3,1;1,2;2,2;3,2;1,3;2,3;3,3;1,1;2,1;3,1];
[unqPairs, ~, pairID] = unique(A,'rows','stable');
pairCount = histcounts(pairID,'BinMethod','integers');
T = table(unqPairs, pairCount(:), 'VariableNames', {'UniquePairs','Count'});
Result
9×2 table
UniquePairs Count
___________ _____
1 1 4
2 1 4
3 1 4
1 2 3
2 2 3
3 2 3
1 3 3
2 3 3
3 3 3