在MATLAB中经济高效地创建对称矩阵Cost-effective creation of a symmetric matrix in MATLAB

``````D = zeros(size(data,1));
for i = 1:size(data, 1)
for j = 1:size(data, 1)
[D(i, j), ~, ~] = graphshortestpath(G, i, j, 'Directed', false);
end
end
``````

3 个回复3

===============>>#1 票数：2 已采纳

``````D = zeros(size(data,1));
for i = 1:size(data, 1)
for j = 1:size(data, 1)
if j > i
[D(i, j), ~, ~] = graphshortestpath(G, i, j, 'Directed', false);
else
D(i, j) = D(j, i);
end
end
end
``````

===============>>#2 票数：1

``````function [D,connect]=genDistance(G,N)

D = G;
B = G;
connect = 0;
i=1;
while((~connect)&&(i<N-1))    % the maximum distance from one vert to another
i = i + 1;                % is N-1
B = B * G;                % G to the power of i
D = D + i * (D==0&B>0);   % D==0 & B>0 the entries to be updated
connect = ( min(min(D)) )>0;  %check if D has zero entry
end
D ( eye(N) ) = 0 ; %clear diagonal entries
``````
• （i，j）“G power k”中的条目将给出从G到Vi的长度k的步数k
• 最后，connect将告诉您图表是否已连接

===============>>#3 票数：0

``````N = length(G);
[irow, icol] = find(tril(ones(N),-1));
``````

``````D = zeros(size(G));
for i = 1:numel(irow)
[D(irow(i), icol(i)] = graphshortestpath(G, irow(i), icol(i), 'Directed', false);
end
D = D + D';
``````

``````D = arrayfun(@(x,y) graphshortestpath(G,x,y, 'Directed', false), irow, icol);
D = squareform(D);
``````

1回复

2回复

2回复

1回复

2回复

1回复

1回复

2回复

2回复

3回复