findnode

Locate node in graph

Description

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.

Examples

collapse all

Create a graph, and then determine the numeric node index for the nodes named 'AB' and '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.

Input Arguments

collapse all

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.

This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.

FormSingle NodeMultiple 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: k = findnode(G,'Chicago') returns the numeric node ID for the node in graph G with the name 'Chicago'.

Data Types: char | cell | string

Output Arguments

collapse all

Numeric node IDs, returned as a scalar or column vector. The values of k are indices into G.Nodes(k,:).

Introduced in R2015b