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!