Extract a subgraph that contains node 'B' and all of its neighbors. subgraph preserves the node names and edge weights. However, the numeric node IDs in H are renumbered compared to G.
N = neighbors(G,'B');
H = subgraph(G, ['B'; N]);
plot(H,'EdgeLabel',H.Edges.Weight)
Input graph, specified as either a graph or digraph
object. Use graph to create an undirected graph or
digraph to create a directed graph.
Example: G = graph(1,2)
Example: G = digraph([1 2],[2 3])
Node identifiers, specified as one or more node indices or node names.
nodeIDs selects a subset of the nodes in
G to 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'})
Node selection vector, specified as a logical vector. The subgraph
contains only the nodes J for which
idx(J) is logical 1
(true). The index of node J in
H is I(J), where I =
find(idx).
Subgraph, returned as a graph or
digraph object. H contains only
the nodes that were selected with nodeIDs or
idx. Other nodes in G (and the
edges connecting to those nodes) are discarded. The node properties and edge
properties of the selected nodes and edges are carried over from
G into H.
See graph or digraph for more information
about graph objects.
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window.
Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.