findgroups
查找组并返回组编号
语法
说明
要将数据分成若干组并将函数应用于这些组,请同时使用 findgroups 和 splitapply 函数。有关数据组计算的详细信息,请参阅对数据组的计算。
返回 G = findgroups(A)G,它是从分组变量 A 生成的组编号的向量。输出参量 G 包含从 1 到 N 的整数值,指示 A 中 N 个唯一值的 N 个不同组。例如,如果 A 是 ["b" "a" "a" "b"],则 findgroups 返回 G 为 [2 1 1 2]。换句话说,G 中的组编号对应于 A 中排序的唯一值。
要使用 G 基于其他变量拆分数据组,请将其作为 splitapply 函数的输入参量传入。
findgroups 函数将 NaN 中的缺失字符串、空字符向量、NaT、A 以及未定义的分类值视为缺失值,并返回 NaN 作为 G 的对应元素。
示例
使用组编号将患者体重测量值分为吸烟者和非吸烟者两个体重组。然后,计算每组患者的平均体重。
从示例文件 patients.mat 中加载患者数据。
load patients whos Smoker Weight
Name Size Bytes Class Attributes Smoker 100x1 100 logical Weight 100x1 800 double
用 findgroups 指定组。G 的每个元素均为组编号,用于指定患者所在的组。组 1 包含非吸烟者,组 2 包含吸烟者。
G = findgroups(Smoker)
G = 100×1
2
1
1
1
1
1
2
1
1
1
1
1
1
2
1
⋮
显示患者的体重。
Weight
Weight = 100×1
176
163
131
133
119
142
142
180
183
132
128
137
174
202
129
⋮
使用 G 将 Weight 数组分成两个体重组。应用 mean 函数。非吸烟者的平均体重略低于吸烟者的平均体重。
meanWeights = splitapply(@mean,Weight,G)
meanWeights = 2×1
149.9091
161.9412
计算各组患者的平均体重。在本例中,根据患者的吸烟或不吸烟状态以及就诊的医院对患者进行分组。数据集中有三家医院,因此有六组患者。
从示例文件 patients.mat 中加载患者的医院位置、是否为吸烟者以及患者体重。
load patients whos Location Smoker Weight
Name Size Bytes Class Attributes Location 100x1 15008 cell Smoker 100x1 100 logical Weight 100x1 800 double
显示 Location 和 Smoker 数组。
Location
Location = 100×1 cell
{'County General Hospital' }
{'VA Hospital' }
{'St. Mary's Medical Center'}
{'VA Hospital' }
{'County General Hospital' }
{'St. Mary's Medical Center'}
{'VA Hospital' }
{'VA Hospital' }
{'St. Mary's Medical Center'}
{'County General Hospital' }
{'County General Hospital' }
{'St. Mary's Medical Center'}
{'VA Hospital' }
{'VA Hospital' }
{'St. Mary's Medical Center'}
{'VA Hospital' }
{'St. Mary's Medical Center'}
{'VA Hospital' }
{'County General Hospital' }
{'County General Hospital' }
{'VA Hospital' }
{'VA Hospital' }
{'VA Hospital' }
{'County General Hospital' }
{'County General Hospital' }
{'VA Hospital' }
{'VA Hospital' }
{'County General Hospital' }
{'County General Hospital' }
{'County General Hospital' }
⋮
Smoker
Smoker = 100×1 logical array
1
0
0
0
0
0
1
0
0
0
0
0
0
1
0
⋮
根据位置和是否为吸烟者指定组。G 包含从 1 到 6 的整数,因为 Smoker 和 Location 中的值有六种可能的组合。
G = findgroups(Location,Smoker)
G = 100×1
2
5
3
5
1
3
6
5
3
1
1
3
5
6
3
⋮
计算每个组的平均体重。是否为吸烟者的患者体重差异要高于不同位置患者的体重差异。
meanWeights = splitapply(@mean,Weight,G)
meanWeights = 6×1
150.1739
159.8125
146.8947
158.4000
152.0417
165.9231
计算各患者组的体重均值,将结果显示在表中。要将体重均值与组 ID 相关联,请使用 findgroups 中的第二个输出参量。
从示例文件 patients.mat 中加载患者体重和是否为吸烟者。
load patients whos Smoker Weight
Name Size Bytes Class Attributes Smoker 100x1 100 logical Weight 100x1 800 double
使用 findgroups 指定组。输出参量 ID 中的值是 findgroups 根据分组变量找到的组的标签。
[G,ID] = findgroups(Smoker)
G = 100×1
2
1
1
1
1
1
2
1
1
1
1
1
1
2
1
⋮
ID = 2×1 logical array
0
1
计算体重均值。创建一个包含体重均值的表。
meanWeight = splitapply(@mean,Weight,G); T = table(ID,meanWeight,VariableNames=["Smokers" "Mean Weights"])
T=2×2 table
Smokers Mean Weights
_______ ____________
false 149.91
true 161.94
计算各患者组的体重均值,将结果显示在表中。在本例中,根据患者的吸烟或不吸烟状态以及就诊的医院对患者进行分组。
从示例文件 patients.mat 中加载患者的医院位置、是否为吸烟者以及患者体重。
load patients whos Location Smoker Weight
Name Size Bytes Class Attributes Location 100x1 15008 cell Smoker 100x1 100 logical Weight 100x1 800 double
将 Location 转换为字符串数组。然后使用位置和是否为吸烟者指定组。您可以指定两个组 ID 作为附加输出,因为您将两个分组变量指定为输入。位置和是否为吸烟者有六种可能的组合。ID1 和 ID2 一起为六个组提供 ID。
Location = string(Location); [G,ID1,ID2] = findgroups(Location,Smoker)
G = 100×1
2
5
3
5
1
3
6
5
3
1
1
3
5
6
3
⋮
ID1 = 6×1 string
"County General Hospital"
"County General Hospital"
"St. Mary's Medical Center"
"St. Mary's Medical Center"
"VA Hospital"
"VA Hospital"
ID2 = 6×1 logical array
0
1
0
1
0
1
计算每个组的平均体重。
meanWeights = splitapply(@mean,Weight,G)
meanWeights = 6×1
150.1739
159.8125
146.8947
158.4000
152.0417
165.9231
创建一个包含每组患者体重均值的表。
T = table(ID1,ID2,meanWeights,VariableNames=["Hospital" "Smoker" "Mean Weight"])
T=6×3 table
Hospital Smoker Mean Weight
___________________________ ______ ___________
"County General Hospital" false 150.17
"County General Hospital" true 159.81
"St. Mary's Medical Center" false 146.89
"St. Mary's Medical Center" true 158.4
"VA Hospital" false 152.04
"VA Hospital" true 165.92
使用表中的分组变量计算患者的体重均值。
将 100 名患者的医院位置和吸烟状态加载到表中。
load patients
T = table(Location,Smoker)T=100×2 table
Location Smoker
_____________________________ ______
{'County General Hospital' } true
{'VA Hospital' } false
{'St. Mary's Medical Center'} false
{'VA Hospital' } false
{'County General Hospital' } false
{'St. Mary's Medical Center'} false
{'VA Hospital' } true
{'VA Hospital' } false
{'St. Mary's Medical Center'} false
{'County General Hospital' } false
{'County General Hospital' } false
{'St. Mary's Medical Center'} false
{'VA Hospital' } false
{'VA Hospital' } true
{'St. Mary's Medical Center'} false
{'VA Hospital' } true
⋮
使用 T 中的 Smoker 和 Location 变量指定各患者组。
G = findgroups(T)
G = 100×1
2
5
3
5
1
3
6
5
3
1
1
3
5
6
3
⋮
根据数据数组 Weight 计算体重均值。
meanWeights = splitapply(@mean,Weight,G)
meanWeights = 6×1
150.1739
159.8125
146.8947
158.4000
152.0417
165.9231
对于按医院位置及是否吸烟者分组的患者,创建一个包含体重均值的表。
将患者的位置和吸烟状态加载到表中。将 Location 转换为字符串数组。
load patients
Location = string(Location);
T = table(Location,Smoker)T=100×2 table
Location Smoker
___________________________ ______
"County General Hospital" true
"VA Hospital" false
"St. Mary's Medical Center" false
"VA Hospital" false
"County General Hospital" false
"St. Mary's Medical Center" false
"VA Hospital" true
"VA Hospital" false
"St. Mary's Medical Center" false
"County General Hospital" false
"County General Hospital" false
"St. Mary's Medical Center" false
"VA Hospital" false
"VA Hospital" true
"St. Mary's Medical Center" false
"VA Hospital" true
⋮
使用 T 中的 Location 和 Smoker 变量指定各患者组。输出表 TID 用于标识各组。
[G,TID] = findgroups(T); TID
TID=6×2 table
Location Smoker
___________________________ ______
"County General Hospital" false
"County General Hospital" true
"St. Mary's Medical Center" false
"St. Mary's Medical Center" true
"VA Hospital" false
"VA Hospital" true
根据数据数组 Weight 计算体重均值。将体重均值追加到 TID。
TID.meanWeight = splitapply(@mean,Weight,G)
TID=6×3 table
Location Smoker meanWeight
___________________________ ______ __________
"County General Hospital" false 150.17
"County General Hospital" true 159.81
"St. Mary's Medical Center" false 146.89
"St. Mary's Medical Center" true 158.4
"VA Hospital" false 152.04
"VA Hospital" true 165.92
输入参数
分组变量,指定为向量。A 中的唯一值标识各组。您可以使用下表中列出的数据类型指定分组变量。
用于指定组的值 | 分组变量的数据类型 |
|---|---|
数字 | 数值或逻辑向量 |
文本 | 字符串数组或字符向量元胞数组 |
日期和时间 |
|
类别 |
|
bin | 分 bin 值的向量,通过对数值、 |
分组变量,指定为表。findgroups 将每个表变量视为一个单独的分组变量。
表变量可以是数值、逻辑值、字符串、categorical、datetime、duration 或 calendarDuration 向量,或是字符向量元胞数组。
输出参量
组编号,以正整数向量形式返回。对于分组变量中标识的 N 个组,介于 1 和 N 之间的每个整数指定一个组。当任何分组变量包含缺失字符串、空字符向量、NaN、 NaT 或未定义的 categorical 值时,G 会在相应位置包含 NaN。
如果分组变量为向量,则
G和分组变量都具有相同的大小。如果分组变量在一个表中,则
G的长度等于该表的行数。
用于标识每个组的值,以输入参量 A 中排序的唯一值的向量形式返回。ID 的数据类型与 A 的数据类型相同。
用于标识每个组的唯一值,以表形式返回。TID 的变量具有来自 T 的对应变量的经过排序的唯一值。但是,TID 和 T 不需要具有相同的行数。
详细信息
在数据分析中,您通常对数据组执行计算。对于这种计算,您可以将一个或多个数据变量拆分成若干数据组,对每个数据组执行计算,并将结果组合成一个或多个输出变量。您可以使用一个或多个分组变量来指定组。分组变量中的唯一值定义数据变量的对应值所属的组。
例如,该图显示简单的分组计算,该计算将 6×1 数值向量拆分为两组数据,计算每组数据的均值,然后将输出合并为一个 2×1 数值向量。6×1 分组变量有两个唯一值,即 AB 和 XYZ。

您可以指定包含数值、文本、日期和时间、类别或 bin 数据的分组变量。
扩展功能
此函数支持 tall 数组,但存在以下限制:
不支持 tall 表。
G中的组编号顺序可能不同于内存中的findgroups计算结果。
有关详细信息,请参阅使用 tall 数组处理无法放入内存的数据。
findgroups 函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2015b 中推出当分组变量或向量类型为数值或 string 时,findgroups 函数的性能得到了改进。
如果分组变量或向量为数值,并且元素总数和每组元素数都很大,则性能改进最为明显。如果分组变量或向量类型为 string,并且元素总数很大且每组元素数很小,则性能改进最为明显。
例如,以下代码将一个长度为 12,500,000 的数值向量中的元素拆分为 25 个编号组。该代码比上一版本大约快 8.9 倍。
function timingTest numberOfGroups = 25; elementsPerGroup = 5e5; groups = repmat(1:numberOfGroups,1,elementsPerGroup)'; G = @() findgroups(groups); t = timeit(G) end
大致的执行时间是:
R2025b:1.15 秒
R2026a:0.13 秒
此代码是在运行 Windows® 11 的 AMD EPYC™ 74F3 24 核处理器 @ 3.19 GHz 测试系统上通过调用 timingTest 函数计时的。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)