How to keep count of occurence of column combination using 'for' loop?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam Danz
2020-6-23
Still fuzzy to me.
Given your example input variable A, what's the expected output?
采纳的回答
Adam Danz
2020-6-23
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!