MATLAB 按类别对数据进行分组

问题描述 投票:0回答:1

我有一个结构体数组,其中每个结构体包含 3 个字段。该阵列如下图所示。这三个字段是“Measured_Signal_lvl”、“BiD”和“SiD”。我想通过 BiD 和 SiD 对数据进行装箱。 BiD 字段可以取 0-7 之间的值,SiD 只能是 [1,4,10,18]。我想将所有相同的 BiD 及其相应的 SiD 放入其各自的组中。所以我希望每个 BiD 都有一个单独的组,应该有 8 个。

到目前为止,我所做的就是将数组转换为 MATLAB 表。然后,我使用“ismember”函数对所有 BiD 进行分组并将它们存储在单独的表中。然后,我重复相同的过程,但在已分组的 BiD 表上获取每个 BiD 表中的所有 SiD。正如您所看到的,这种方法效率不太高,并且下面发布的代码非常重复。我想在代码和架构方面采用更有效的方法。

结构数组快照:

Snapshot of the structure array

我目前拥有的代码:

% Find all the BiD == 0 
bId_0 = st(ismember(st.BiD,0),:);

% Find all the BiD == 1 
bId_1 = st(ismember(st.BiD,1),:);

% Find all the BiD == 2 
bId_2 = st(ismember(st.BiD,2),:);

% Find all the BiD == 3 
bId_3 = st(ismember(st.BiD,3),:);

% Find all the BiD == 4 
bId_4 = st(ismember(st.BiD,4),:);

% Find all the BiD == 5 
bId_5 = st(ismember(st.BiD,5),:);

% Find all the BiD == 6 
bId_6 = st(ismember(st.BiD,6),:);

% Find all the BiD == 7 
bId_7 = st(ismember(st.BiD,7),:);

% % Drill Further to find the SIDs per BIDs
% % all BId 0s, SiD 1,4,10,18
bId_0_sId_1 = bId_0(ismember(bId_0.SiD,1),:);
bId_0_sId_4 = bId_0(ismember(bId_0.SiD,4),:);
bId_0_sId_10 = bId_0(ismember(bId_0.SiD,10),:);
bId_0_sId_18 = bId_0(ismember(bId_0.SiD,18),:);
% all BId 1s, SiD 1,4,10,18
bId_1_sId_1 = bId_1(ismember(bId_1.SiD,1),:);
bId_1_sId_4 = bId_1(ismember(bId_1.SiD,4),:);
bId_1_sId_10 = bId_1(ismember(bId_1.SiD,10),:);
bId_1_sId_18 = bId_1(ismember(bId_1.SiD,18),:);
% all BiD 2s, SiD 1,4,10,18
bId_2_sId_1 = bId_2(ismember(bId_2.SiD,1),:);
bId_2_sId_4 = bId_2(ismember(bId_2.SiD,4),:);
bId_2_sId_10 = bId_2(ismember(bId_2.SiD,10),:);
bId_2_sId_18 = bId_2(ismember(bId_2.SiD,18),:);
% all BiD 3s, SiD 1,4,10,18
bId_3_sId_1 = bId_3(ismember(bId_3.SiD,1),:);
bId_3_sId_4 = bId_3(ismember(bId_3.SiD,4),:);
bId_3_sId_10 = bId_3(ismember(bId_3.SiD,10),:);
bId_3_sId_18 = bId_3(ismember(bId_3.SiD,18),:);
% all BiD 4s, SiD 1,4,10,18
bId_4_sId_1 = bId_4(ismember(bId_4.SiD,1),:);
bId_4_sId_4 = bId_4(ismember(bId_4.SiD,4),:);
bId_4_sId_10 = bId_4(ismember(bId_4.SiD,10),:);
bId_4_sId_18 = bId_4(ismember(bId_4.SiD,18),:);
% all BiD 5s, SiD 1,4,10,18
bId_5_sId_1 = bId_5(ismember(bId_5.SiD,1),:);
bId_5_sId_4 = bId_5(ismember(bId_5.SiD,4),:);
bId_5_sId_10 = bId_5(ismember(bId_5.SiD,10),:);
bId_5_sId_18 = bId_5(ismember(bId_5.SiD,18),:);
% all BiD 6s, SiD 1,4,10,18
bId_6_sId_1 = bId_6(ismember(bId_6.SiD,1),:);
bId_6_sId_4 = bId_6(ismember(bId_6.SiD,4),:);
bId_6_sId_10 = bId_6(ismember(bId_6.SiD,10),:);
bId_6_sId_18 = bId_6(ismember(bId_6.SiD,18),:);
% all BiD 7s, SiD 1,4,10,18
bId_7_sId_1 = bId_7(ismember(bId_7.SiD,1),:);
bId_7_sId_4 = bId_7(ismember(bId_7.SiD,4),:);
bId_7_sId_10 = bId_7(ismember(bId_7.SiD,10),:);
bId_7_sId_18 = bId_7(ismember(bId_7.SiD,18),:);
matlab structure
1个回答
0
投票

一个可能的解决方案是使用:

[g, BiD_SiD_groups] = findgroups(st.BiD, st.SiD);

那么,

groupedData = splitapply(@(varargin) {st(varargin{1}, :)}, (1:height(st))', g);

现在您可以使用 for 循环访问不同的情况:

BiD_val= 0:7;
SiD_val = [1,4,10,18];
for ii=1:numel(BiD_val)  
    for jj=1:numel(SiD_val) 
        groupIndex = find(BiD_SiD_groups(:,1) == BiD_val(ii) & BiD_SiD_groups(:,2) == SiD_val(jj));
        data{ii,jj} = groupedData{groupIndex}; % the answer... 
    end
end
© www.soinside.com 2019 - 2024. All rights reserved.