This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Reverse edge directions


H = flipedge(G)
H = flipedge(G,s,t)
H = flipedge(G,idx)



H = flipedge(G) returns a directed graph that has the same edges as G, but with reversed directions. H contains the same node and edge properties as G.


H = flipedge(G,s,t) reverses a subset of edges using the node pairs s and t.

H = flipedge(G,idx) reverses a subset of edges using the edge indices idx.


collapse all

Create and plot a directed graph. Then reverse the direction of all of the edges in the graph.

G = digraph([1 1 1 1],[2 3 4 5]);

H = flipedge(G);

Create and plot a directed graph. Specify custom xy node coordinates for the plot.

G = digraph([1 1 2 2 3],[2 3 3 4 5]);
x = [1 0 2 -1 3];
y = [1 2 2 2.5 2.5];

Reverse the direction of the edges (2,4) and (3,5), and then replot the graph.

H = flipedge(G,[2 3],[4 5]);

Input Arguments

collapse all

Input graph, specified as a digraph object. Use digraph to create a directed graph object.

Example: G = digraph([1 2],[2 3])

Node pairs, specified as separate arguments of node indices or node names. Similarly located elements in s and t specify the source and target nodes for edges in the graph.

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


Example: 1


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 = flipedge(G,[1 4],[2 3]) reverses the direction of the edges (1,2) and (4,3).

Example: H = flipedge(G,{'a' 'd'},{'b' 'c'}) reverses the direction of the edges (a,b) and (d,c).

Edge indices, specified as a scalar or vector of positive integers. Each edge index corresponds to a row in the G.Edges table of the graph G.Edges(idx,:).

Example: H = flipedge(G,3) flips the direction of edge G.Edges(3,:).

Output Arguments

collapse all

Output graph, returned as a digraph object. Compared to G, H has the same nodes but some or all of the edges are in reversed direction. H also has the same node and edge properties as G.

Introduced in R2016b