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

4 views (last 30 days)

Show older comments

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]);

##### 0 Comments

### Accepted Answer

Jiri Hajek
on 1 Dec 2022

##### 3 Comments

Jiri Hajek
on 1 Dec 2022

OK, this would be the natural sequence to get aajacency matrix that correspponds to your map of nodes:

- Define the positions of your nodes
- From the positions, calculate their distances
- Use the distances to find which nodes are neighbours
- Define the graph object
- 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.

### 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.

### See Also

### Community Treasure Hunt

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

Start Hunting!