Extract a subgraph that contains node'B'and all of its neighbors.subgraphpreserves the node names and edge weights. However, the numeric node IDs inHare renumbered compared toG.
N = neighbors(G,'B'); H = subgraph(G, ['B'; N]); plot(H,'EdgeLabel',H.Edges.Weight)
输入graph, specified as either agraphordigraphobject. Usegraphto create an undirected graph ordigraphto create a directed graph.
Example:G = graph(1,2)
Example:G = digraph([1 2],[2 3])
nodeIDs—Node identifiers node indices|node names
Node identifiers, specified as one or more node indices or node names.nodeIDsselects a subset of the nodes inGto generate the subgraph,H.
This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.
Form
Single Node
Multiple Nodes
Node index
Scalar
Example:1
Vector
Example:[1 2 3]
Node name
Character vector
Example:'A'
Cell array of character vectors
Example:{'A' 'B' 'C'}
String scalar
Example:"A"
String array
Example:["A" "B" "C"]
Example:H = subgraph(G,[1 2 5])
Example:H = subgraph(G,{'A' 'B' 'E'})
idx—Node selection vector vector
Node selection vector, specified as a logical vector. The subgraph contains only the nodesJfor whichidx(J)is logical1(true). The index of nodeJinHisI(J), whereI = find(idx).
Subgraph, returned as agraphordigraphobject.Hcontains only the nodes that were selected withnodeIDsoridx. Other nodes inG(and the edges connecting to those nodes) are discarded. The node properties and edge properties of the selected nodes and edges are carried over fromGintoH.
Seegraphordigraphfor more information about graph objects.
Extended Capabilities
C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The node identifiersnodeIDsmust be node indices of the input graphG. Node names are not supported.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.