问题描述
我在2D坐标空间中具有网络表示形式.我有一个邻接矩阵Adj
(稀疏)和一个coordinate
矩阵,其中绘制了图中所有点/节点/顶点的x,y值.
I have the network representation in a 2D coordinate space. I have an adjacency matrix Adj
(which is sparse) and a coordinate
matrix with the x,y values of all the points/nodes/vertices in the graph which are drawn.
我想尽可能有效地计算这些点之间的距离.我想避免遍历矩阵中的条目并一一计算成对距离.
I would like to compute as efficiently as possible the distance between these points. I would like to avoid cycling through the entries in the matrix and computing the pairwise distances one by one.
推荐答案
[n, d] = size(coordinate);
assert(d == 2);
resi = sparse(Adj * diag(1:n));
resj = sparse(diag(1:n) * Adj);
res = sparse(zeros(n));
f = find(Adj)
res(f) = sqrt((coordinate(resi(f), 1) - coordinate(resj(f), 1)) .^ 2 + (coordinate(resi(f), 2) - coordinate(resj(f), 2)) .^ 2);
糟糕,修复了一个错误
澄清:我假设通过座标矩阵表示的意思是 http://www.passagesoftware. net/webhelp/Coordinate_Matrix.htm
Clarification: I'm assuming by coordinate matrix you mean like http://www.passagesoftware.net/webhelp/Coordinate_Matrix.htm
我实际上不确定Adj是否是逻辑矩阵(或者您是否可以拥有逻辑的稀疏矩阵).我将其修复以避免潜在的陷阱
EDIT 2: I'm actually not sure whether Adj is a matrix of logicals (or whether you can have a sparse matrix of logicals). I fixed it to sidestep that potential pitfall
这篇关于MATLAB-使用邻接矩阵和坐标计算图/网络中点之间距离的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!