How do I connect two points from my Adjacency Matrix that are k distance away from each other and display it in my graph?

3 visualizaciones (últimos 30 días)
I have created an adjacency matrix with random values. I am able to plot all the values with edges that connect to and from each and every single point.
I have attempted to remove the edges from those that have weights greater than 5 but in doing so I get a weird output.
I have attached the part of my code that handles creating and plotting the adjacency matrix (I used gplot), as well as, my output. You will quickly notice that there are a lot of nodes right next to each other but they do not have an edge connected to each other.
It should be that if a node is within a certain distance to another node then keep the edge, otherwise remove the edge.
Only those within my specified distance which is defined in my "dist" variable on line 3.
numOfVertices = 50;
NxNSize = 50;
dist = 5; % I only want nodes within this distance to have an edge.
XYCoordinates = rand(numOfVertices, 2);
A = randi(numOfVertices, NxNSize, NxNSize); % A = randi(generate random number of nodes
% between 1 and n, number of rows, number of columns);
G = graph(A, 'upper');
AdjacencyMat = adjacency(G, "weighted");
AdjacencyMat(AdjacencyMat(:,:) > dist) = 0; % If the weight of your edge is greater than the value
% stored in the "dist" variable, then remove it.
% This part of my code formats the marker size and color, as well as, the size of the window
x0 = 20;
y0 = 20;
width = 700;
height = 700;
gplot(AdjacencyMat, XYCoordinates, 'g.-'); % Plot the adjacency matrix
% More formatting that shouldn't affect the output, only the aesthetic
set(groot,'defaultLineMarkerSize', 9);
set(gca, 'Color', 'k');
set(gcf,'position',[x0,y0,width,height]);

Respuesta aceptada

Jiri Hajek
Jiri Hajek el 1 de Dic. de 2022
Hi, it seems you have mixed up two concepts together. The meaning of a nonzero element (i,j) in the adjacency matrix is that connection between i-th and j-the node exists. Then, you postulated that the value of an element on the adhjacency matrix is the distance, when you compared the value of elements with the threshold. But independently of the adjacency matrix, you have also created a list of random positions (which you did not use to calculate the distances)... So it is no surprise, that the positions are not reflected by ytour adjacency matrix...
  3 comentarios
Jiri Hajek
Jiri Hajek el 1 de Dic. de 2022
OK, this would be the natural sequence to get aajacency matrix that correspponds to your map of nodes:
  1. Define the positions of your nodes
  2. From the positions, calculate their distances
  3. Use the distances to find which nodes are neighbours
  4. Define the graph object
  5. Construct the adjacency matrix for your graph
Starting with a random adjacency matrix would not give you the positions of nodes, only their distances... Your original code makes two conflicting definitions - you define positions and adjacency independently, in random.

Iniciar sesión para comentar.

Más respuestas (1)

Bruno Luong
Bruno Luong el 1 de Dic. de 2022
"You will quickly notice that there are a lot of nodes right next to each other but they do not have an edge connected to each other."
You seem to confuse between the weight (stored in A) and position distance (stored in XYCoordinates ), you only "remove" edge with weight > 5. But you plot them using XYCoordinates, they are generated independently so there are NO correlation between edge removed and distance of nodes in the graph.

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by