matlab请教以下程序有什么问题 50
mat=R;n=length(mat);m=sum(sum(mat))/2;vs=[1:n];%vsaretheverticeswhichneedtoberecalcul...
mat=R;
n=length(mat);
m=sum(sum(mat))/2;
vs=[1:n]; %vs are the vertices which need to be recalculate
for i1=1:m
smat=zeros(n);
%calculate the shortest-path betweenness
for i2=1:length(vs)
%BFS
ln=0; %number of leaves
D=zeros(1,length(vs))-1;
D(vs(i2))=0;
W(vs(i2))=1;
pah=1;pat=1; %two pointers indicate the index of array
array(pat)=vs(i2);
pat=pat+1;
%calculate the distances and weight of vertices
while(pah<pat)
tp=array(pah);
leaf=1; %assume tp is a leaf
for i3=1:length(vs)
if(mat(vs(tp),vs(i3)))
if(D(vs(i3))==-1)
leaf=0; %tp isn't a leaf
array(pat)=vs(i3);
pat=pat+1;
D(vs(i3))=D(vs(tp))+1;
W(vs(i3))=W(vs(tp));
elseif(D(vs(i3))==D(vs(tp))+1)
W(vs(i3))=W(vs(i3))+W(vs(tp));
end
end
end
if(leaf)
ln=ln+1;
leaves(ln)=tp;
end
pah=pah+1;
end
%calculate the betweenness of the edges
tvs=vs;
for i3=1:length(tvs) %sort vs by des of D
for i4=i3+1:length(tvs)
if(D(tvs(i3))<D(tvs(i4)))
t=tvs(i3);
tvs(i3)=tvs(i4);
tvs(i4)=t;
end
end
end
tmat=zeros(n); %temp matrix to save the betweenness
T=zeros(1,n); %temp array to save the sum of betweenness of the lower edges of a vertex
for i3=1:length(tvs)
tp=tvs(i3);
for i4=1:length(tvs)
if(mat(tvs(i3),tvs(i4)) && D(tvs(i3))==D(tvs(i4))-1 && D(tvs(i3))~=-1)
T(tvs(i3))=T(tvs(i3))+tmat(tvs(i3),tvs(i4));
end
end
for i4=1:length(tvs)
if(mat(tvs(i3),tvs(i4)) && D(tvs(i3))==D(tvs(i4))+1 && D(tvs(i4))~=-1)
tmat(tvs(i3),tvs(i4))=(T(tvs(i3))+1)*W(tvs(i4))/W(tvs(i3));
tmat(tvs(i4),tvs(i3))=tmat(tvs(i3),tvs(i4));
end
end
end
smat=smat+tmat;
end
%find the max-betweenness edge
t=0;
for i2=1:n
for i3=1:n
if(smat(i2,i3)>t)
t=smat(i2,i3);
cut(i1,:)=[i2 i3 smat(i2,i3)];
end
end
end
%save('tmat.txt','mat','-ascii');
mat(cut(i1,1),cut(i1,2))=0;
mat(cut(i1,2),cut(i1,1))=0;
end
cut
为什么运行后出现??? Attempted to access cut(511,1); index out of bounds because size(cut)=[510,3].
R是一个只有0和1的矩阵 展开
n=length(mat);
m=sum(sum(mat))/2;
vs=[1:n]; %vs are the vertices which need to be recalculate
for i1=1:m
smat=zeros(n);
%calculate the shortest-path betweenness
for i2=1:length(vs)
%BFS
ln=0; %number of leaves
D=zeros(1,length(vs))-1;
D(vs(i2))=0;
W(vs(i2))=1;
pah=1;pat=1; %two pointers indicate the index of array
array(pat)=vs(i2);
pat=pat+1;
%calculate the distances and weight of vertices
while(pah<pat)
tp=array(pah);
leaf=1; %assume tp is a leaf
for i3=1:length(vs)
if(mat(vs(tp),vs(i3)))
if(D(vs(i3))==-1)
leaf=0; %tp isn't a leaf
array(pat)=vs(i3);
pat=pat+1;
D(vs(i3))=D(vs(tp))+1;
W(vs(i3))=W(vs(tp));
elseif(D(vs(i3))==D(vs(tp))+1)
W(vs(i3))=W(vs(i3))+W(vs(tp));
end
end
end
if(leaf)
ln=ln+1;
leaves(ln)=tp;
end
pah=pah+1;
end
%calculate the betweenness of the edges
tvs=vs;
for i3=1:length(tvs) %sort vs by des of D
for i4=i3+1:length(tvs)
if(D(tvs(i3))<D(tvs(i4)))
t=tvs(i3);
tvs(i3)=tvs(i4);
tvs(i4)=t;
end
end
end
tmat=zeros(n); %temp matrix to save the betweenness
T=zeros(1,n); %temp array to save the sum of betweenness of the lower edges of a vertex
for i3=1:length(tvs)
tp=tvs(i3);
for i4=1:length(tvs)
if(mat(tvs(i3),tvs(i4)) && D(tvs(i3))==D(tvs(i4))-1 && D(tvs(i3))~=-1)
T(tvs(i3))=T(tvs(i3))+tmat(tvs(i3),tvs(i4));
end
end
for i4=1:length(tvs)
if(mat(tvs(i3),tvs(i4)) && D(tvs(i3))==D(tvs(i4))+1 && D(tvs(i4))~=-1)
tmat(tvs(i3),tvs(i4))=(T(tvs(i3))+1)*W(tvs(i4))/W(tvs(i3));
tmat(tvs(i4),tvs(i3))=tmat(tvs(i3),tvs(i4));
end
end
end
smat=smat+tmat;
end
%find the max-betweenness edge
t=0;
for i2=1:n
for i3=1:n
if(smat(i2,i3)>t)
t=smat(i2,i3);
cut(i1,:)=[i2 i3 smat(i2,i3)];
end
end
end
%save('tmat.txt','mat','-ascii');
mat(cut(i1,1),cut(i1,2))=0;
mat(cut(i1,2),cut(i1,1))=0;
end
cut
为什么运行后出现??? Attempted to access cut(511,1); index out of bounds because size(cut)=[510,3].
R是一个只有0和1的矩阵 展开
1个回答
展开全部
错误的原因是矩阵访问越界
if(smat(i2,i3)>t)
t=smat(i2,i3);
cut(i1,:)=[i2 i3 smat(i2,i3)];
end
这句的意思是当smat(i2,i3)>t才会对cut赋值,当不满足要求时
mat(cut(i1,1),cut(i1,2))=0;
mat(cut(i1,2),cut(i1,1))=0;
这一句在条件不满足的情况下,访问的值是不存在的
可以把这两句放到判断下的循环中
if(smat(i2,i3)>t)
t=smat(i2,i3);
cut(i1,:)=[i2 i3 smat(i2,i3)];
end
这句的意思是当smat(i2,i3)>t才会对cut赋值,当不满足要求时
mat(cut(i1,1),cut(i1,2))=0;
mat(cut(i1,2),cut(i1,1))=0;
这一句在条件不满足的情况下,访问的值是不存在的
可以把这两句放到判断下的循环中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询