Please find attached matlab code for the above problem.
How, in Matlab, can i traverse a tree and save node numbers in a separate matrix in required order?
3 次查看(过去 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
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!