1

我在 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
...
=============================

在上述情况下,我想对相同的数据速率求和srcdest并获得一个新的结构数组,如下所示:

=============================
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。但我很确定有一个简单的方法还没有击中我。

有人可以帮我解决这个问题。

4

3 回答 3

4

一种方法是识别唯一行unique,然后使用一些逻辑索引来组合它们的数据速率。

例如:

% 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数据类型,您可以更轻松地使用findgroupssplitapply获得相同的结果。

使用上面的相同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;

这也返回:

 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      
于 2017-03-16T11:21:49.850 回答
1

您可以将前两个字段组合成一个字符串并用于'src'创建索引,用于 1)提取唯一集(如下)和 2)将数据速率与(如下)相加:'dest'uniqueindex1accumarrayindex2

% 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
于 2017-03-16T16:17:29.837 回答
-3

dest使用您定义的唯一性创建一个新结构。
从这里您有 2 个选项:

  1. 遍历源结构的所有字段并将它们添加到新结构中。
  2. 使用 MATLAB 的索引。

第一个更快。
第二个要慢得多,但代码可能更像 MATLAB 样式。

于 2017-03-16T11:16:49.733 回答