Convert Array to Adjacency matrix
21 views (last 30 days)
Show older comments
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 ?
0 Comments
Answers (1)
Guillaume
on 11 Mar 2016
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)
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!