How to discover the neighbor nodes in an matrix?

조회 수: 7 (최근 30일)
Fatos Sulo
Fatos Sulo 2022년 12월 8일
댓글: Jon 2022년 12월 8일
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
John D'Errico
John D'Errico 2022년 12월 8일
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 2022년 12월 8일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jon
Jon 2022년 12월 8일
편집: Jon 2022년 12월 8일
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
Jon
Jon 2022년 12월 8일
Note also that MATLAB graphs even allow finding nearest neighbors within a radius, so you could probably even further simplify the above.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by