A = adjacency(G)
returns the sparse adjacency matrix for graph G. If
(i,j) is an edge in G, then
A(i,j) = 1. Otherwise, A(i,j) =
0.

A = adjacency(G,'weighted')
returns a weighted adjacency matrix, where for each edge
(i,j), the value A(i,j) contains the
weight of the edge. If the graph has no edge weights, then
A(i,j) is set to 1. For this syntax, G
must be a simple graph such that ismultigraph(G) returns
false.

A = adjacency(G,weights)
returns a weighted adjacency matrix with edge weights given by the vector
weights. For each edge (i,j) in
G, the adjacency matrix has value A(i,j) =
weights(findedge(G,i,j)). For this syntax, G
must be a simple graph such that ismultigraph(G) returns
false.

Create a directed graph using an edge list, and then find the equivalent adjacency matrix representation of the graph. The adjacency matrix is returned as a sparse matrix.

s = [1 1 1 2 2 3];
t = [2 3 4 5 6 7];
G = digraph(s,t)

G =
digraph with properties:
Edges: [6x1 table]
Nodes: [7x0 table]

Create an undirected graph using an upper triangular adjacency matrix. When constructing a graph with an adjacency matrix, the nonzero values in the matrix correspond to edge weights.

A = [0 5 3 0;0 0 1 2; 0 0 0 11; 0 0 0 0]

A = 4×4
0 5 3 0
0 0 1 2
0 0 0 11
0 0 0 0

G = graph(A,'upper')

G =
graph with properties:
Edges: [5x2 table]
Nodes: [4x0 table]

Use adjacency to return the adjacency matrix of the graph. Regardless of the form of adjacency matrix used to construct the graph, the adjacency function always returns a symmetric and sparse adjacency matrix containing only 1s and 0s.

Preview a full storage version of the matrix. Since G is a directed graph, the adjacency matrix is not symmetric. However, the adjacency matrix is symmetric for undirected graphs.

Adjacency matrix, returned as a sparse matrix. The size of
A is
numnodes(G)-by-numnodes(G).

Tips

Edges with weight zero are not visible in the sparse adjacency matrix returned
by adjacency. This means that a weighted adjacency matrix
can represent a weighted graph only if there are no edges of weight zero.

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.