Clear Filters
Clear Filters

How to keep count of occurence of column combination using 'for' loop?

1 view (last 30 days)
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.
  4 Comments
Adam Danz
Adam Danz on 23 Jun 2020
Still fuzzy to me.
Given your example input variable A, what's the expected output?
Chaithra D
Chaithra D on 23 Jun 2020
Okay fine..
how to check the number of occurence count of pair(1,1) in matrix 'A'?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Jun 2020
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

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!