How do I connect two points from my Adjacency Matrix that are k distance away from each other and display it in my graph?
5 views (last 30 days)
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(gca, 'Color', 'k');
Jiri Hajek on 1 Dec 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...
More Answers (1)
Bruno Luong on 1 Dec 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.