필터 지우기
필터 지우기

How to fix this code? It doesnt give me an edge from 1 to 2! what is the problem in my code?

조회 수: 1 (최근 30일)
p = [3,2,1];
for i = 1:length(p)
last_element = p(end);
G = graph();
H = addnode(G,last_element);
p(end) = []; % REMOVING THE LAST ELEMENT ONCE ADDED TO THE GRAPH so in 2nd iteration p = [3,2]
disp(p);
if(isempty(p))
break;
else
disp(p(end));
V = addedge(G,last_element,p(end));
end
plot(V);
end

답변 (2개)

Walter Roberson
Walter Roberson 2018년 6월 9일
I do not see any "hold" command, so the plot(V) is going to overwrite the plotted graph in each iteration.
You are deliberately creating a new graph() in each iteration of i, so it seems that you do not want to create one graph that has all of the edges together, only one edge at a time, but you are not displaying the individual graphs into different figures or subplots...
  댓글 수: 1
Aswin Sandirakumaran
Aswin Sandirakumaran 2018년 6월 10일
Yea. SO how to fix it? so that in each iteration we can just add those in the plots and to obtain the final one containing all iteration? because in this case it just outputs my final iteration, it overwrites the on done previously. How to fix it? Can you help me?

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2018년 6월 10일
You want a graph with three nodes and two edges, those edges being (3, 2) and (2, 1)? You can do this with one call to graph.
p = [3 2 1];
sources = p(1:end-1)
targets = p(2:end)
g = graph(sources, targets)
plot(g)
You don't necessarily need to create the intermediate arrays sources and targets, I just did it so you can see what they contain.
p = [3 2 1];
g = graph(p(1:end-1), p(2:end))
plot(g)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by