Cosine Similarity and distances between nodes

5 visualizaciones (últimos 30 días)
Christian Basa
Christian Basa el 16 de Abr. de 2021
Respondida: Austin Thai el 17 de Abr. de 2021
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?

Respuestas (1)

Austin Thai
Austin Thai el 17 de Abr. de 2021
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

Categorías

Más información sobre Construction en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by