MATLAB: Splitapply help with logicals

beginnerfindgrouplogical?MATLABsplitapplytable

Hello!
I need some help from this. Again, after thinking, I came up with a table like this.
Yes/No Genders
___________ __________
Yes female
No male
No they
Yes female
yes male
So I guess the question here would be "Have you ever identified yourself as any of these genders at any given time?"
And given the data I want my output to be like this:
Yes/No Female Male They
___________ _______ _____ _____
Yes 16 13 33
No 20 22 7
So basically the genders theyve identifed as would be turned into their own columns.
I was inspired by these mathworks pages:
So these are my thoughts so far:
  • the Yes/No would be logical since they're esstienally true/false
  • of course split apply would be used here.
  • I was also thinking I could use @sum to find the sum of yes and no of the Genders instead of making my own functions since I am not confident in that yet
So what should be my first steps? Here is an idea I have:
MaleT= nnz(ismember(T2.genders(T2.response == true), 'male'))
FemaleF = nnz(ismember(T2.genders(T2.response == false), 'female'))
% repeat above for each true and false of the genders
Not sure what next steps to take from here…

Best Answer

groupsummary() does that but in a different format.
Demo
T = table(categorical({'Y';'N';'N';'Y';'Y'}),...
categorical({'F';'M';'T';'F';'M'}), 'variablenames', {'YN','Gender'})
% T =
% 5×2 table
% YN Gender
% __ ______
% Y F

% N M
% N T
% Y F
% Y M
GS = groupsummary(T,{'YN','Gender'})
% GS =
% 4×3 table
% YN Gender GroupCount
% __ ______ __________
% N M 1
% N T 1
% Y F 2
% Y M 1
Number of "yes" answers associated with "female":
GS.GroupCount(GS.YN=='Y' & GS.Gender=='F')
% ans =
% 2
Related Question