Directed Graph with Node IDs as String

조회 수: 3 (최근 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 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