Cosine Similarity and distances between nodes

7 ビュー (過去 30 日間)
Christian Basa
Christian Basa 2021 年 4 月 16 日
回答済み: Austin Thai 2021 年 4 月 17 日
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 Center および File ExchangeConstruction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by