필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Chaithra D
Chaithra D 2020년 6월 23일
답변: Adam Danz 2020년 6월 23일
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
Adam Danz 2020년 6월 23일
Still fuzzy to me.
Given your example input variable A, what's the expected output?
Chaithra D
Chaithra D 2020년 6월 23일
Okay fine..
how to check the number of occurence count of pair(1,1) in matrix 'A'?

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by