How to discover the neighbor nodes in an matrix?

7 views (last 30 days)
Hello guys,
I'm new here and I need your help. I have this code that I need to translate it into Matlab. I have an idea to create a matrix of distances between nodes, where for example: d11is the distanace between 1 and 1 (which will be 0), d12 is the distance of 1 with 2. Then checking the first row of the matrix compares the distance values with the transmission radius of node 1 and if it is smaller than the radius then these nodes will be included in the vector of neighbors of the first node. this is my idea but I don't know how to write it in Matlab. If someone could help me, I would be very grateful.
  2 Comments
John D'Errico
John D'Errico on 8 Dec 2022
I had to laugh when I read what you posted. Clearly, the authors do not test their own code. I see that sometimes they write declare, but at other times they use the declear tool. I wonder if there is a difference? I wonder what declear does anyway?
And is that Preducre, or Preduce? Or just maybe is it supposed to be Procedure? The point is, I doubt I would trust such a poorly written/edited/proofread text to be accurate.
Jan
Jan on 8 Dec 2022
John hits the point.
There is a "Neighbor list" as input, a "Neighbor_list" and empty array, a "MyNeighborsSet" and "Neighbors". The pseudocode is not useful and you cannot implement it in Matlab reliably.

Sign in to comment.

Answers (1)

Jon
Jon on 8 Dec 2022
Edited: Jon on 8 Dec 2022
Here is a little example that I think does what you are asking
%
% assign threshold for being a neighbor
dMax = 0.6;
% make an example distance matrix where i,j entry gives distance from node
% i to node j
D = rand(5,5);
% make the example matrix symmetric
D = (D + D')/2
% make main diagonal zero, nodes are always neighbors of themselves
D = D - diag(diag(D));
% find the neighbors of each node
% form logical matrix whose (i,j)th entries are true if node j is a
% neighbor of node i
isNeighbor = D <= dMax;
% make neighbor lists
% use cell array because lists may have different lengths
numNodes = size(D,1);
Neighbor = cell(numNodes,1);
for k = 1:numNodes
Neighbor{k} = find(isNeighbor(k,:));
end
display(Neighbor)
You could also do this using MATLAB's graph functions, replacing the last lines of the above code with
% could also do this using a graph
G = graph(isNeighbor);
Neighbor = cell(numNodes,1);
for k = 1:numNodes
Neighbor{k} = neighbors(G,k)';
end
display(Neighbor);
  1 Comment
Jon
Jon on 8 Dec 2022
Note also that MATLAB graphs even allow finding nearest neighbors within a radius, so you could probably even further simplify the above.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!