How to add nodes and display it as a graph??

3 ビュー (過去 30 日間)
Aswin Sandirakumaran
Aswin Sandirakumaran 2018 年 6 月 28 日
編集済み: Stephan 2018 年 6 月 28 日
G = graph();
j = [1,2,3,4,5];
for i = 1:length(i)
H = addnode(G,j(i));
plot(H);
end
QUESTION: How to plot graph with all the nodes of j ?*
MY PROBLEM IS: IT just adds the node 1 which is 1, but it doesnt add other nodes 2,3,4,5
  1 件のコメント
Guillaume
Guillaume 2018 年 6 月 28 日
編集済み: Guillaume 2018 年 6 月 28 日
What exactly are you trying to do? Why are you adding the nodes in a loop rather than all at once (or better create G with the nodes)? Are the j values supposed to be node ids or the number of nodes to add (so is it 5 or 15 nodes that need adding)?

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

回答 (1 件)

Stephan
Stephan 2018 年 6 月 28 日
編集済み: Stephan 2018 年 6 月 28 日
Hi,
one issue is, that you wrote:
for i = 1:length(i)
instead of:
for i = 1:length(j)
Since length(i) is equal to 1 you only got one node. Additionally the plot command can be set behind the for loop.
P.S.: It is good practice not to use i and j as Counter variables, because that can cause Trouble using complex numbers.
For a more detailled answer to this topic see the answer from Guillaume below.
Best regards
Stephan
  2 件のコメント
Guillaume
Guillaume 2018 年 6 月 28 日
編集済み: Guillaume 2018 年 6 月 28 日
There are a lot more problems with the original code than just length(j) vs length(i) unfortunately.
Note that while the answer appears to do the correct thing, what it actually does is exactly equivalent to:
G = graph;
j = 1:5;
H = addnode(G, i(end));
plot(H);
This is because
  • H is overwritten at each step of the loop, discarding the result of all but the last iteration
  • addnode(G, x) adds x nodes to G. It does not add a node with number x to G
Stephan
Stephan 2018 年 6 月 28 日
Thanks for the correction - I withdraw the contribution.

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

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by