Use the treeplot function
5 次查看(过去 30 天)
显示 更早的评论
hey
I want to apply the treeplot function. My goal is to generate a tree like I illustrated in attached picture.
I don't know how to use the correct syntax.
I hope someone can help me.
Best
0 个评论
回答(1 个)
Steven Lord
2018-5-3
Number your nodes 1 through 11. Create a vector with 11 elements. Each element in the vector should contain the number of the node that is the parent of the node with that element number (or 0 if that node has no parent.) For example if node 2 was the parent of node 6, element 6 of the vector would be 2.
If you want the nodes to be laid out more closely to how they appear in that picture, with all children of the red 1 being level with the red 2, etc. consider building a digraph and calling plot on the digraph. Specifying the 'layered' layout looks pretty close to the picture you posted, but if you want to get even closer you can explicitly specify the X and Y coordinates for your nodes.
7 个评论
Steven Lord
2018-5-3
If you open the documentation in the installation you're using and go to the Mathematics section of the MATLAB documentation, one of the subsections in that section should be Graph and Network Algorithms. That subsection page includes a number of Topics that describe how to create, manipulate, query, and visualize graph and digraph objects.
You can use the techniques from those Topics pages and the examples on the graph and digraph pages to convert your raw "city X is connected to city Y" data into one of these objects and then use the functions that allow you to ask questions (like "Is there an edge from A to B?", findedge, or "What's the shortest path from C to D?", shortestpath) on the graph or digraph object and/or the functions that allow you to manipulate (adding a new city with addnode or removing an edge between two cities with rmedge) that object.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!