Count occurrences of categorical conjunction

조회 수: 2 (최근 30일)
Nex
Nex 2021년 9월 28일
답변: Chunru 2021년 9월 28일
Given two separate categorical variables with the same length (refering to the same data):
Cat_one = categorical({'A'; 'B'; 'A'; 'D'; 'C'; 'B'; 'B'; 'A'; 'A'});
Cat_two = categorical({'X'; 'X'; 'Y'; 'Y'; 'Y'; 'X'; 'Y'; 'X'; 'X'});
How can I find the average occurences of each unique item in Cat_one within group X of Cat_two?

채택된 답변

Chunru
Chunru 2021년 9월 28일
Cat_one = categorical({'A'; 'B'; 'A'; 'D'; 'C'; 'B'; 'B'; 'A'; 'A'});
Cat_two = categorical({'X'; 'X'; 'Y'; 'Y'; 'Y'; 'X'; 'Y'; 'X'; 'X'});
u1 = unique(Cat_one);
for i=1:length(u1)
idx = Cat_one == u1(i);
c(i) = sum(Cat_two(idx) == 'X');
end
c % corresponding occurence of 'X'; Not sure the definition of averaging
c = 1×4
3 2 0 0

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by