matlab运行出现问题,Attempted to access e(1,0); index must be a positive integer or logical.高手帮我看看以下程序吧,dijkstra算法,记录任意两点之间的最短路径,if netCostMatrix(t,farthestPreviousHop(t)) < netCostMatrix(t,p)

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/12 00:46:33
matlab运行出现问题,Attempted to access e(1,0); index must be a positive integer or logical.高手帮我看看以下程序吧,dijkstra算法,记录任意两点之间的最短路径,if netCostMatrix(t,farthestPreviousHop(t)) < netCostMatrix(t,p)

matlab运行出现问题,Attempted to access e(1,0); index must be a positive integer or logical.高手帮我看看以下程序吧,dijkstra算法,记录任意两点之间的最短路径,if netCostMatrix(t,farthestPreviousHop(t)) < netCostMatrix(t,p)
matlab运行出现问题,Attempted to access e(1,0); index must be a positive integer or logical.
高手帮我看看以下程序吧,dijkstra算法,记录任意两点之间的最短路径,if netCostMatrix(t,farthestPreviousHop(t)) < netCostMatrix(t,p) 在这行出现问题
function [path,totalCost,farthestPreviousHop,farthestNextHop] = dijkstra_1(n,netCostMatrix,farthestPreviousHop,farthestNextHop)
visited(1:n) = 0;
distance(1:n) = inf; % it stores the shortest distance between each node and the source node;
parent(1:n) = 0;
fid=fopen('allpath.txt','w');
for s = 1:100
distance(s) = 0;
for i = 1:(n-1),
temp = [];
for h = 1:n,
if visited(h) == 0 % in the tree;
temp=[temp distance(h)];
else
temp=[temp inf];
end
end;
[t,u] = min(temp); % it starts from node with the shortest distance to the source;
visited(u) = 1; % mark it as visited;
for v = 1:n,% for each neighbors of node u;
if ( ( netCostMatrix(u,v) + distance(u)) < distance(v) )
distance(v) = distance(u) + netCostMatrix(u,v); % update the shortest distance when a shorter path is found;
parent(v) = u; % update its parent;
end;
end;
end;
for d = s:100,
path = [];
if parent(d) = 0 % if there is a path!
%path = [];
t = d;
path = [d];
while t = s
p = parent(t);
path = [p path];
if netCostMatrix(t,farthestPreviousHop(t)) < netCostMatrix(t,p)
farthestPreviousHop(t) = p;
end;
if netCostMatrix(p,farthestNextHop(p)) < netCostMatrix(p,t)
farthestNextHop(p) = t;
end;
t = p;
end;
totalCost= distance(d);
fprintf(fid,'%d ',path); %记录源点到任意一点的最短路径
end;
%allpath(d)=path;
end
fprintf(fid,'\n');
end
fclose(fid);
return;
s是起始节点,d是目标节点
令s=1时,程序没有问题,但是加上循环for s = 1:100 时,

matlab运行出现问题,Attempted to access e(1,0); index must be a positive integer or logical.高手帮我看看以下程序吧,dijkstra算法,记录任意两点之间的最短路径,if netCostMatrix(t,farthestPreviousHop(t)) < netCostMatrix(t,p)
nit PublicTree;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
ComCtrls,ImgList,DB,Menus;
type
TFrameTree = class(TFrame)
tvTree:TTreeView;
ImageList1:TImageList;
procedure tvTreeExpanding(Sender:TObject; Node:TTreeNode;
var AllowExpansion:Boolean);