假设我们有一个名为tf.Tensor
的变量,其中包含分组信息
group
我们必须为每对group = [ {1}, {2}, ..., {D} ]
维护一个计数器,其中i,j在[1 D]范围内,对于所有(i, j)
对,这些对合并。
示例:
count(i, j) >= 5
因此合并了对,结果组是
D = 5
group = [ {1}, {2}, {3}, {4}, {5} ]
(1, 2) = 7
(2, 3) = 10
(3, 4) = 2
(4, 5) = 20
在我的问题group = [ {1 2 3}, {4 5} ]
中可以有D
的值,实现此逻辑的有效方法是什么
答案 0 :(得分:1)
您可以使用count>=5
作为邻接矩阵并生成graph
:
% generating random count matrix
D = 20;
count = zeros(D);
count( randsample(D^2,round(D/2)) ) = 10;
% find connections
A = count >= 5;
% A is symmetric - if (1,2) so (2,1) as well
A = A | A';
% build graph
G = graph(A);
% get connected components labeling from graph
idxs = G.conncomp;
% generate groups of nodes
nodes = num2cell(1:max(idxs));
group = cellfun(@(g) find(g == idxs),nodes,'UniformOutput',0);
% plot graph
plot(G)