Create a graph from a matrix with 1 and 0
12 views (last 30 days)
Show older comments
Hello,
Let's suppose that I have this matrix:
1 1 0 0 0 1
1 1 1 0 0 0
1 1 1 0 0 0
1 0 0 0 1 1
1 0 0 0 0 0
0 1 1 1 0 1
I want to convert this matrix to a graph, where each node should be a specific zero from the matrix.
The final goal should be to find the minimum cost from a start node to a final node.
My difficulty is how can I 'convert' this matrix to a graph, of course, with the creation of an adjacency matrix.
My 'idea' is to go through the matrix line with line, and each 0 element will be a node . For example, for the first line I have the nodes: 1, 2 3. For the second line, I have nodes 4,5,6. Line 3, nodes:7,8,9. Line 4, nodes:10,11,12. Line 5, nodes:13,14,15,16,17 and finally, line 6, nodes 18 and 19. For every edge between two nodes the ideea was to see how much 'energy' it takes to move from one node to another, and this would be the cost from a node to another.
Can someone help me to achieve this goal?
Thank you
0 Comments
Answers (1)
Walter Roberson
on 30 Mar 2020
Adj = [1 1 0 0 0 1
1 1 1 0 0 0
1 1 1 0 0 0
1 0 0 0 1 1
1 0 0 0 0 0
0 1 1 1 0 1];
G = digraph(Adj);
plot(G);
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!