Matlab根据条件求和结构数组行
Matlab sum struct array rows based on criteria
我在Matlab中有一个结构数组如下:
temp_links = struct('src',{},'dest',{}, 'type', {}, 'datarate', {});
temp_links
中的数据如下:
===============================
src dest type datarate
================================
sw_1 sw_2 sw 23
sw_1 sw_2 sw 34
sw_1 sw_2 sw 2
sw_1 sw_2 sw 3
sw_1 sw_3 sw 5
sw_1 sw_3 sw 8
sw_1 sw_3 sw 9
sw_1 sw_3 sw 3
sw_1 sw_3 sw 23
sw_1 sw_3 sw 20
sw_2 dev1 dev 30
sw_2 dev1 dev 20
...
=============================
在上述情况下,我想对相同的 src
和 dest
的数据速率求和并得到一个新的结构数组,如下所示:
=============================
src dest type datarate
================================
sw_1 sw_2 sw 62
sw_1 sw_3 sw 68
sw_1 dev1 dev 50
...
=============================
我对如何实现这一目标感到困惑。
我的想法是为每个 src 字段设置一个 switch case,然后填充 dest。但我很确定有一种简单的方法还没有击中我。
谁能帮我解决这个问题。
使用您定义的唯一 dest
创建一个新结构。
从这里您有 2 个选择:
- 遍历源结构的所有字段并将它们添加到新结构。
- 使用 MATLAB 的索引。
第一个更快。
第二个要慢得多,但代码可能更像 MATLAB 风格。
一种方法可能是使用 unique
and then use some logical indexing 组合它们的数据速率来识别唯一行。
例如:
% Sample Data
temp_links = struct('src',{'sw_1', 'sw_1', 'sw_1', 'sw_2', 'sw_2', 'sw_2'}, ...
'dest',{'sw_2', 'sw_2', 'sw_3', 'sw_1', 'dev_1', 'dev_1'}, ...
'type', {'sw', 'sw', 'sw', 'sw', 'dev', 'dev'}, ...
'datarate', {23, 34, 2, 5, 5, 5} ...
);
% Locate and index each unique source, destination, and type
[src_nodes, ~, src_idx] = unique({temp_links(:).src});
[dest_nodes, ~, dest_idx] = unique({temp_links(:).dest});
[types, ~, type_idx] = unique({temp_links(:).type});
% Combine the indices and use to locate and index unique rows
row_layout = [src_idx, dest_idx, type_idx];
[unique_rows, ~, row_idx] = unique(row_layout, 'rows');
% Initialize results table based on the unique rows
joined_links = struct('src', {src_nodes{unique_rows(:,1)}}, ...
'dest', {dest_nodes{unique_rows(:,2)}}, ...
'type', {types{unique_rows(:,3)}}, ...
'datarate', [] ...
);
% Sum data rates for identical rows
for ii = 1:size(unique_rows, 1)
joined_links(ii).datarate = sum([temp_links(row_idx==ii).datarate]);
end
对于我们的示例输入结构:
src dest type datarate
______ _______ _____ ________
'sw_1' 'sw_2' 'sw' 23
'sw_1' 'sw_2' 'sw' 34
'sw_1' 'sw_3' 'sw' 2
'sw_2' 'sw_1' 'sw' 5
'sw_2' 'dev_1' 'dev' 5
'sw_2' 'dev_1' 'dev' 5
我们收到以下连接结构:
src dest type datarate
______ _______ _____ ________
'sw_1' 'sw_2' 'sw' 57
'sw_1' 'sw_3' 'sw' 2
'sw_2' 'dev_1' 'dev' 10
'sw_2' 'sw_1' 'sw' 5
或者,如果您想使用 MATLAB 的 Table
datatype you can more easily utilize findgroups
and splitapply
来获得相同的结果。
使用上面相同的 temp_links
结构:
temp_links = struct2table(temp_links);
groups = findgroups(temp_links.src, temp_links.dest, temp_links.type);
combined_datarate = splitapply(@sum, temp_links.datarate, groups);
[unique_groups, idx] = unique(groups);
joined_links = temp_links(idx, :);
joined_links.datarate = combined_datarate;
其中还有 returns:
src dest type datarate
______ _______ _____ ________
'sw_1' 'sw_2' 'sw' 57
'sw_1' 'sw_3' 'sw' 2
'sw_2' 'dev_1' 'dev' 10
'sw_2' 'sw_1' 'sw' 5
您可以将前两个字段 'src'
和 'dest'
组合成一个字符串并使用 unique
to create indices for 1) extracting unique sets (index1
below) and 2) summing the data rates with accumarray
(下面的 index2
):
% Sample data (from excaza's answer):
temp_links = struct('src',{'sw_1', 'sw_1', 'sw_1', 'sw_2', 'sw_2', 'sw_2'}, ...
'dest',{'sw_2', 'sw_2', 'sw_3', 'sw_1', 'dev_1', 'dev_1'}, ...
'type', {'sw', 'sw', 'sw', 'sw', 'dev', 'dev'}, ...
'datarate', {23, 34, 2, 5, 5, 5});
% Get indices for unique src/dest combinations:
[~, index1, index2] = unique([strvcat(temp_links.src) strvcat(temp_links.dest)], 'rows');
unique_links = temp_links(index1); % Get subset of structure array
datarate = num2cell(accumarray(index2, [temp_links.datarate])); % Sum datarates
[unique_links.datarate] = datarate{:}; % Add datarate sums to subarray
我在Matlab中有一个结构数组如下:
temp_links = struct('src',{},'dest',{}, 'type', {}, 'datarate', {});
temp_links
中的数据如下:
===============================
src dest type datarate
================================
sw_1 sw_2 sw 23
sw_1 sw_2 sw 34
sw_1 sw_2 sw 2
sw_1 sw_2 sw 3
sw_1 sw_3 sw 5
sw_1 sw_3 sw 8
sw_1 sw_3 sw 9
sw_1 sw_3 sw 3
sw_1 sw_3 sw 23
sw_1 sw_3 sw 20
sw_2 dev1 dev 30
sw_2 dev1 dev 20
...
=============================
在上述情况下,我想对相同的 src
和 dest
的数据速率求和并得到一个新的结构数组,如下所示:
=============================
src dest type datarate
================================
sw_1 sw_2 sw 62
sw_1 sw_3 sw 68
sw_1 dev1 dev 50
...
=============================
我对如何实现这一目标感到困惑。 我的想法是为每个 src 字段设置一个 switch case,然后填充 dest。但我很确定有一种简单的方法还没有击中我。
谁能帮我解决这个问题。
使用您定义的唯一 dest
创建一个新结构。
从这里您有 2 个选择:
- 遍历源结构的所有字段并将它们添加到新结构。
- 使用 MATLAB 的索引。
第一个更快。
第二个要慢得多,但代码可能更像 MATLAB 风格。
一种方法可能是使用 unique
and then use some logical indexing 组合它们的数据速率来识别唯一行。
例如:
% Sample Data
temp_links = struct('src',{'sw_1', 'sw_1', 'sw_1', 'sw_2', 'sw_2', 'sw_2'}, ...
'dest',{'sw_2', 'sw_2', 'sw_3', 'sw_1', 'dev_1', 'dev_1'}, ...
'type', {'sw', 'sw', 'sw', 'sw', 'dev', 'dev'}, ...
'datarate', {23, 34, 2, 5, 5, 5} ...
);
% Locate and index each unique source, destination, and type
[src_nodes, ~, src_idx] = unique({temp_links(:).src});
[dest_nodes, ~, dest_idx] = unique({temp_links(:).dest});
[types, ~, type_idx] = unique({temp_links(:).type});
% Combine the indices and use to locate and index unique rows
row_layout = [src_idx, dest_idx, type_idx];
[unique_rows, ~, row_idx] = unique(row_layout, 'rows');
% Initialize results table based on the unique rows
joined_links = struct('src', {src_nodes{unique_rows(:,1)}}, ...
'dest', {dest_nodes{unique_rows(:,2)}}, ...
'type', {types{unique_rows(:,3)}}, ...
'datarate', [] ...
);
% Sum data rates for identical rows
for ii = 1:size(unique_rows, 1)
joined_links(ii).datarate = sum([temp_links(row_idx==ii).datarate]);
end
对于我们的示例输入结构:
src dest type datarate
______ _______ _____ ________
'sw_1' 'sw_2' 'sw' 23
'sw_1' 'sw_2' 'sw' 34
'sw_1' 'sw_3' 'sw' 2
'sw_2' 'sw_1' 'sw' 5
'sw_2' 'dev_1' 'dev' 5
'sw_2' 'dev_1' 'dev' 5
我们收到以下连接结构:
src dest type datarate
______ _______ _____ ________
'sw_1' 'sw_2' 'sw' 57
'sw_1' 'sw_3' 'sw' 2
'sw_2' 'dev_1' 'dev' 10
'sw_2' 'sw_1' 'sw' 5
或者,如果您想使用 MATLAB 的 Table
datatype you can more easily utilize findgroups
and splitapply
来获得相同的结果。
使用上面相同的 temp_links
结构:
temp_links = struct2table(temp_links);
groups = findgroups(temp_links.src, temp_links.dest, temp_links.type);
combined_datarate = splitapply(@sum, temp_links.datarate, groups);
[unique_groups, idx] = unique(groups);
joined_links = temp_links(idx, :);
joined_links.datarate = combined_datarate;
其中还有 returns:
src dest type datarate
______ _______ _____ ________
'sw_1' 'sw_2' 'sw' 57
'sw_1' 'sw_3' 'sw' 2
'sw_2' 'dev_1' 'dev' 10
'sw_2' 'sw_1' 'sw' 5
您可以将前两个字段 'src'
和 'dest'
组合成一个字符串并使用 unique
to create indices for 1) extracting unique sets (index1
below) and 2) summing the data rates with accumarray
(下面的 index2
):
% Sample data (from excaza's answer):
temp_links = struct('src',{'sw_1', 'sw_1', 'sw_1', 'sw_2', 'sw_2', 'sw_2'}, ...
'dest',{'sw_2', 'sw_2', 'sw_3', 'sw_1', 'dev_1', 'dev_1'}, ...
'type', {'sw', 'sw', 'sw', 'sw', 'dev', 'dev'}, ...
'datarate', {23, 34, 2, 5, 5, 5});
% Get indices for unique src/dest combinations:
[~, index1, index2] = unique([strvcat(temp_links.src) strvcat(temp_links.dest)], 'rows');
unique_links = temp_links(index1); % Get subset of structure array
datarate = num2cell(accumarray(index2, [temp_links.datarate])); % Sum datarates
[unique_links.datarate] = datarate{:}; % Add datarate sums to subarray