我在Matlab中使用无线网络。我已经创建了联系人表,这意味着两个节点有联系以及联系人的开始和结束时间。联系表在 Matlab 中采用结构的形式,如下所示:
contact(1)
node_1:23
node_2:76
t_start: 45
t_end: 58
假设这是我的联系表的第一个条目。现在我需要将此条目转换为具有以下形式的单元格数组:
45 CONN 23 76 up
58 CONN 23 76 down
或者,以更通用的形式编写它:
t_start CONN node_1 node_2 up
t_end CONN node_1 node_2 down
我需要以这种特定形式导出它们并将它们使用到一个模拟器。所以我的问题是如何在 Matlab 中转换它?我知道结构中存在许多条目,单元格数组的大小将翻倍,例如对于 50 个条目,单元格数组中将有 100 行,但我不知道该怎么做。
因此,您需要使用arrayfun
为每个元素生成数据结构。然后将这些连接在一起。
% Anonymous function that creates a data structure for ONE struct entry
func = @(c){c.t_start, 'CONN', c.node_1, c.node_2, 'up'; ...
c.t_end, 'CONN', c.node_1, c.node_2, 'down'};
% Now perform this on ALL struct elements and concatenate the result.
data = arrayfun(func, contact, 'uniform', 0);
data = cat(1, data{:})
因此,如果在您的示例中,我们创建两个相同的contact
只是为了测试它。
contact = repmat(contact, [2, 1]);
我们会得到
data =
[45] 'CONN' [23] [76] 'up'
[58] 'CONN' [23] [76] 'down'
[45] 'CONN' [23] [76] 'up'
[58] 'CONN' [23] [76] 'down'