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