Cosine Similarity and distances between nodes

9 次查看(过去 30 天)
Is it possible to use cosine similarity to find the distances on a graph between a node and other nodes surrounding it? For example, I have 80 nodes. I find the distance from node 1 to nodes 2:80 and then node 2 from node 1 and nodes 3:80 and repeat that process until i get all the distances?

回答(1 个)

Austin Thai
Austin Thai 2021-4-17
I assume you are trying to calculate the cosine distance using the cosine similarity.
You can use a for loop , e.g.
nodalCoordinates=rand(80,3); % Replace these with your coordinates
nodalNorms=vecnorm(nodalCoordinates,2,2);
cosineDistances=zeros(80,80); % Include the distance to itself (zero) for simplicity
for i=1:80
cosineSimilarity=nodalCoordinates*nodalCoordinates(i,:)'./(nodalNorms(i)*nodalNorms);
cosineDistances(i,:)=1-cosineSimilarity;
end
If you simply want the spatial distance,
spatialDistances=zeros(80,80); % Include the distance to itself (zero) for simplicity
for i=1:80
spatialDistances(i,:)=vecnorm(nodalCoordinates-nodalCoordinates(i,:),2,2);
end

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by