Count occurrences of categorical conjunction

2 views (last 30 days)
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?

Accepted Answer

Chunru
Chunru on 28 Sep 2021
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

More Answers (0)

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!