Directed Graph with Node IDs as String

1 回表示 (過去 30 日間)
Arun Subramanian
Arun Subramanian 2016 年 11 月 9 日
コメント済み: Arun Subramanian 2016 年 11 月 10 日
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 件のコメント
Arun Subramanian
Arun Subramanian 2016 年 11 月 10 日
Thanks a lot. It did the trick for me.

サインインしてコメントする。

その他の回答 (1 件)

Eng. Fredius Magige
Eng. Fredius Magige 2016 年 11 月 9 日
Hi Gplot with adjacency matrix is good option for multidirective graph, Thanks

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by