Directed Graph with Node IDs as String

2 vues (au cours des 30 derniers jours)
Arun Subramanian
Arun Subramanian le 9 Nov 2016
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.

Réponse acceptée

Steven Lord
Steven Lord le 9 Nov 2016
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 commentaire
Arun Subramanian
Arun Subramanian le 10 Nov 2016
Thanks a lot. It did the trick for me.

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by