www.pudn.com > anttsp.rar > dist_cand.m, change:2008-04-20,size:637b


function [dist,city_num,cand_list]=dist_cand(X,Y,cand_length) 
%计算城市距离矩阵和每一个城市的最近邻列表 
city_num=numel(X); 
for i=1:city_num 
    for j=1:city_num 
        dist(i,j)=sqrt((X(i)-X(j))^2+(Y(i)-Y(j))^2); 
    end  
end 
if (nargin==3)&&(nargin==3) 
    for i=1:city_num 
        temp1=dist(i,:); 
        temp2=sort(temp1);                            %城市i与所有城市的距离非递减排序 
        for j=1:cand_length 
            for k=1:city_num 
                if(temp2(j+1)==temp1(k))              %由于temp2(1)始终为0 
                    cand_list(i,j)=k; 
                end 
            end 
        end 
    end 
end