Count occurrences of categorical conjunction

2 次查看(过去 30 天)
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