我想计算有向图中非相邻节点之间的测地距离。如下矩阵(W)中,零代表这些节点不相邻,其他值表示边对应节点的权重。我根据以下公式计算了距离:
我使用了 MATLAB 函数 graphshortestpath()
。但是,恐怕这个功能无法提供我想要的东西。那么问题是计算这样的距离的另一种方法是什么? MATLAB 中有其他函数可以实现这一点吗?使矩阵稀疏是否有可能影响最终结果?
程序代码:
N=6; % Size of matrix W
W=[0,0.797944993710195,0,0,0,0;0.495326358306295,0,0.164911895411107,0,0,0;0,0.0530273831645896,0,0.00901073533222818,0,0;0,0,0.00709165683115063,0,0.438584093809830,0;0,0,0,0.397895339311598,0,0.000916573989905329;0,0,0,0,0.00104307323830613,0]; %Connectivity matrix
Geo_dist=zeros(N); %temporary variable for geodesic distance measurement
W_sparse=sparse(W); %Making W sparse because the matlab function works only with sparse matrix
for g=1:N
for h=1:N
if W(g,h)==0 & g~=h;
Geo_dist(g,h)=graphshortestpath(W_sparse,g,h,'directed',false);
end
end
end
最佳答案
可以直接在图像网格上使用图像处理工具箱中的bwdistgeodesic或graydist吗?
https://www.mathworks.com/help/images/ref/graydist.html https://www.mathworks.com/help/images/ref/bwdistgeodesic.html
或者,如果您喜欢更平滑的东西,imsegfmm 将允许您使用快速行进方法来计算测地距离,如果您查看文档示例:
https://www.mathworks.com/help/images/ref/imsegfmm.html
我在回复中假设您有网格图像数据,并且这不是任意连接的图表,因为它被标记为“图像处理”。
关于matlab - 在 MATLAB 中计算测地距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57115777/