필터 지우기
필터 지우기

How, in Matlab, can i traverse a tree and save node numbers in a separate matrix in required order?

조회 수: 6 (최근 30일)
I am using Matlab to implement my project. Please help me with the following.
In Matrix A (Figure above), elements of any particular row represent the nodes that are within the communication range of the node represented by the row number. For example node 2 and 12 are with in node 1's communication range, 3 and 13 are within 2's range, 4 is in node 12's range and so on. The tree(or network) in the picture is drawn using matrix A.
I want matrix M for each node x where the first row of M contains the nodes that are within the range of node x(lets call this set of node S1), the second row of M should contain all those nodes that are within the range of any of the nodes in S1(lets call this set S2), the third row should contain all the nodes that are within the range of the nodes in S2 and so on. Right side of the image shows some examples of the required matrices. Both the input and output matrices are size nxn and contain 0's to represent null(For simplicity, I removed unnecessary rows and columns i.e those with all zero's).

채택된 답변

Sam McDonald
Sam McDonald 2017년 3월 6일
The code that was attached in the comment does perform the required calculations to construct each M matrix as described in the problem. For convenience to others in the community, I have re-attached the file to this answer.
Alternatively, you could convert the A matrix into a graph object and use the functions available for graph and network algorithms. There may be a way to use those functions to achieve the same result. The following link provides more information on these functions:
To convert the A matrix into a directed graph object, execute the following code:
[r, c] = size(A);
B = reshape(A, 1, r*c);
n = ones(r, c).*(1:r)';
n = reshape(n, 1, r*c);
i = find(B == 0);
n(i) = []
B(i) = []
G = digraph(n, B);
plot(G)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by