简体   繁体   English

如何在 MATLAB 中按分类变量值对表的数据进行分组?

[英]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. "Gender" ,其值为 0 和 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 .我想根据Gender创建两个包含人们年龄的表。 So one table for the ages of people with Gender == 0 and another for the ages of people with Gender == 1 .因此,一张表用于Gender == 0人的年龄,另一张表用于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!任何帮助将不胜感激,在此先感谢!

I don't know about the groupfilter function but you can not use something as:我不知道 groupfilter 功能,但您不能使用以下内容:

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,:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM