1.How to get (s,t, weights) to generate a graph? The way i used to generate them causes graph error?2.2.Is it possible keep a for loop inside the if statement to take k values only for d(i,j)<tr ,if how to write the statement?

조회 수: 1 (최근 30일)
%n=number of nodes and d(i,j)is the distance between all nodes should be less than transmitting range tr(to establish connection between nodes)
N=round((n*n-1)/2)
s=zeros(1,N);
t=zeros(1,N);
weights=zeros(1,N);
k=1;
for i=1:n
for j=(i+1):n
if d(i,j)<tr
D(1,k)=d(i,j);
s(1,k)=i;
t(1,k)=j;
end
k=k+1
end
end
G = graph(s,t,weights)
plot(G)
  댓글 수: 1
Sneha Kolapalli
Sneha Kolapalli 2017년 4월 11일
편집: Sneha Kolapalli 2017년 4월 11일
1.Error using matlab.internal.graph.MLGraph Source must be a dense double array of node indices.
Error in matlab.internal.graph.constructFromEdgeList (line 125) G = underlyingCtor(s, t, totalNodes);
Error in graph (line 264) matlab.internal.graph.constructFromEdgeList(...
2.Is it possible keep a for loop inside the if statement to take k values only for d(i,j)<tr ,if how to write the statement.

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

답변 (1개)

Steven Lord
Steven Lord 2017년 4월 12일
As Christine Tobler commented in this post, I suspect your s and/or t inputs to graph contain one or more 0 elements. Try trimming the excess points that you didn't fill with data after your loop.
% Fill in part of a vector
% This is a "toy" example; there are better ways to create the final x vector
x = zeros(1, 10);
k = 1;
for z = 1:7
x(k) = z;
k = k+1;
end
disp(x)
% Trim unused elements
x(k:end) = [];
disp(x)

카테고리

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