Search code examples
matlabgrouping

How to group table's data by categorical variable values in MATLAB?


I have a table and I wish to group the data from the table based on the values of a categorical variable. For example, let's say I have the following columns in a table called "data":

  • "Gender", which has the values 0 and 1.
  • "Age", a continuous variable with a wide range of numbers.

I would like to create two tables with the ages of the people based on their Gender. So one table for the ages of people with Gender == 0 and another for the ages of people with Gender == 1. I want all the rows from the table which meet the conditions, not a summary of the data.

I have tried doing the following but it will only return empty tables:

data_m = groupfilter(data,"Gender",@(x) (x) == 0)
data_f = groupfilter(data,"Gender",@(x) (x) == 1)

Any help would be greatly appreciated, thanks in advance!


Solution

  • I don't know about the groupfilter function but you can not use something as:

    name = ["Maria" "Jose" "Arnaldo" "Eva" "Schawarza" "Rose"]';´
    gender = [0 1 1 0 0 0]';
    T = table(name,gender)
    
    male = T(T.gender==1,:)
    female = T(T.gender==0,:)