Directed Graph with Node IDs as String

1 次查看(过去 30 天)
Hi. I am new to graphs in Matlab and started learning it by programming directly. So I have this situation in which one node will be connected to Multiple nodes in a directed graph. I was able to achieve this by using 'digraph' in Matlab. However, the problem comes when I use Node IDs which are string. I am able to achieve it in a single line of code for numerical Node IDs but not for String Node IDs. That is:
G = digraph([1 1] , [2 3]);
plot(G)
I am able to get a directed Graph with three nodes 1, 2 and 3 and the edges being from 1 to 2 and 1 to 3.
When I perform the following code, I get the same result
G = digraph(1 , [2 3]);
plot(G)
However, if i change the numbers to string, I am unable to get a similar result. This is the code I wrote
G = digraph(['v1'] , ['v2' 'v3']);
plot(G)
It gives me only two nodes: v1 and v2v3 and only one edge from v1 to v2v3. But I want is as three nodes and 2 edges:
v1 to v2
v1 to v3
PS: A comma between v2 and v3 did not help.
Please note that I had to write multiple line of code to achieve this but I would like to know if there is a way to put the code in a single line.
G = digraph('v1' , 'v2');
G = addedge(G, 'v1' , 'v3');
plot(G)
This gives me the desired result. But I would like to know if there is a way to do it in a single line.

采纳的回答

Steven Lord
Steven Lord 2016-11-9
When you write the command ['v2' 'v3'] the result is 'v2v3', a 1-by-4 char array. The two char arrays are concatenated together into one larger char array. Instead, use a cell array containing char arrays.
>> G = digraph({'v1'} , {'v2' 'v3'});

更多回答(1 个)

Eng. Fredius Magige
Hi Gplot with adjacency matrix is good option for multidirective graph, Thanks

类别

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