Locate node in graph
k = findnode(G,nodeID)
example
k = findnode(G,nodeID) returns the numeric node ID, k, of the node in graph G whose name or index is nodeID. The numeric node ID is zero if the node is not in the graph.
k
G
nodeID
collapse all
Create a graph, and then determine the numeric node index for the nodes named 'AB' and 'BC'.
'AB'
'BC'
s = {'AA' 'AA' 'AA' 'AB' 'AC' 'BB'}; t = {'BA' 'BB' 'BC' 'BA' 'AB' 'BC'}; G = graph(s,t)
G = graph with properties: Edges: [6x1 table] Nodes: [6x1 table]
k = findnode(G,{'AB' 'BC'})
k = 2×1 5 4
k contains the row index into G.Nodes.Name for each specified node.
G.Nodes.Name
graph
digraph
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)
G = graph(1,2)
Example: G = digraph([1 2],[2 3])
G = digraph([1 2],[2 3])
Node identifiers, specified as one or more node indices or node names.
This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.
Scalar
Example: 1
1
Vector
Example: [1 2 3]
[1 2 3]
Character vector
Example: 'A'
'A'
Cell array of character vectors
Example: {'A' 'B' 'C'}
{'A' 'B' 'C'}
String scalar
Example: "A"
"A"
String array
Example: ["A" "B" "C"]
["A" "B" "C"]
Example: k = findnode(G,'Chicago') returns the numeric node ID for the node in graph G with the name 'Chicago'.
k = findnode(G,'Chicago')
'Chicago'
Data Types: char | cell | string
char
cell
string
Numeric node IDs, returned as a scalar or column vector. The values of k are indices into G.Nodes(k,:).
G.Nodes(k,:)
digraph | findedge | graph | numnodes
findedge
numnodes
You have a modified version of this example. Do you want to open this example with your edits?