Filter löschen
Filter löschen

Count occurrences of categorical conjunction

2 Ansichten (letzte 30 Tage)
Nex
Nex am 28 Sep. 2021
Beantwortet: Chunru am 28 Sep. 2021
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?

Akzeptierte Antwort

Chunru
Chunru am 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

Weitere Antworten (0)

Kategorien

Mehr zu Categorical Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by