2017-04-26 115 views
0

假設我們有一個包含分組被叫group可變信息設計邏輯用於在Matlab的變量分組

group = [ {1}, {2}, ..., {D} ] 

我們必須維持一個計數器,用於每對(i, j)其中i,j是在[1 d的範圍]和所有這些對,其中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可以有1000值,什麼是實現這個的有效途徑邏輯

+0

的對僅連續的數字或任何數字在'[1,d]'間隔?例如,一對可以是「(2,5)」? – user2999345

+0

@ user2999345對不需要連續 – Atinesh

回答

1

您可以使用count>=5作爲鄰接矩陣a第二生成:

% 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) 

enter image description here

+0

在哪個庫中定義'圖形'函數 – Atinesh

+0

我認爲[圖形](https://www.mathworks.com/help/matlab/ref/graph.html)是基本的MATLAB ,但於2015年推出。 – user2999345

+0

真是巧合,我正在使用2015a :) – Atinesh