# Create a graph with several isolated nodes and a few links

1 Ansicht (letzte 30 Tage)
Sim am 14 Okt. 2023
Kommentiert: Sim am 16 Okt. 2023
Why I am not able to create a graph with several isolated nodes and a few links?
s = [1
1
1];
t = [1
2
3];
XY = [ 1 1.5
1.2 1.7
1.3 1.3
1.7 2.2
1 2
4.2 2.9
3.5 2.5
3.8 2.9
3.8 3.2
3.6 3.1
3 3
3.4 3.4
3 2.3];
G = graph(s,t);
G.Nodes.X = XY(:,1); G.Nodes.Y = XY(:,2);
Error using .
To assign to or create a variable in a table, the number of rows must match the height of the table.

Error in graph/subsasgn>assignToNodes (line 161)
nodeprop = subsasgn(nodeprop, S, V);

Error in indexing (line 18)
G = assignToNodes(G, S, V);
##### 6 Kommentare4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Dyuman Joshi am 14 Okt. 2023
But there are no isolated nodes in your graph -
s = [1
1
1];
t = [1
2
3];
G = graph(s,t);
plot(G)
Sim am 16 Okt. 2023
Hi @Dyuman Joshi! Thanks for your comments! I think @Steven Lord got my point..... Sorry, I think I did not explain well my issue... :-)

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Steven Lord am 14 Okt. 2023
Your code would work with one minor addition, a line that adds the additional nodes to the graph that are not present in the s and t vectors you use to create the graph.
s = [1
1
1];
t = [1
2
3];
XY = [ 1 1.5
1.2 1.7
1.3 1.3
1.7 2.2
1 2
4.2 2.9
3.5 2.5
3.8 2.9
3.8 3.2
3.6 3.1
3 3
3.4 3.4
3 2.3];
G = graph(s,t);
G.Nodes.X = XY(:,1);
G.Nodes.Y = XY(:,2);
Let's plot the graph and see the isolated nodes.
plot(G, 'XData', G.Nodes.X, 'YData', G.Nodes.Y)
If later on you want to connect some of those isolated nodes to others, use the addedge function.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Sim am 16 Okt. 2023
Thanks a lot! This is what I was looking for :-)

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Directed Graphs finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by