Convert Array to Adjacency matrix

24 次查看(过去 30 天)
vignesh raju
vignesh raju 2016-2-24
回答: Guillaume 2016-3-11
M = [
0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 1
0 0 1 0 1 0 1 1 0 1 1 0 0 1 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 1 0 1 1 0 1 0 0 1 1 0
1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 0 1 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1
0 0 1 1 1 0 0 1 0 1 0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 1
0 1 1 1 1 0 1 1 0 1 1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 1 1 1 0 0 1 0 1 1 0 0 1 0 0 0 0
0 0 1 1 1 1 0 1 0 1 0 0 0 0 1 0 0 0
]
I have the following array M which I need to convert to adjacency matrix. Can someone guide me as to how this needs to be done ?

回答(1 个)

Guillaume
Guillaume 2016-3-11
If your matrix represents which vertices are connected to which with 1, then it is already an adjacency matrix. If not, then you'll have to give more details about what you want.
Note that since 2015b, matlab has some functions that make it easier to manipulate graphs with the graph and digraph classes.
g = digraph(M); %since your matrix is not symmetrical it must be a directed graph
g.adjacency
Note that the output of the above is simply the sparse representation of M, so you can achieve the same output with any version of matlab with:
sparse(M)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by