用matlab求解邻接矩阵
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/18 09:41:55
用matlab求解邻接矩阵
比如说我的txt文档里有不同线路的公交车经过的站点,用matlab导入txt文档,并求各个站点的邻接矩阵
G002
z107-z552-z477-z478-z480-z190-z527-z398-z253-z251-z114-z589-z332-z329-z183-z006-z040-z051-z224-z178-z434-z437-z462-z104-z455-z411-z624-z229-z384-z577-z283-z167-z217
G030
z068-z348-z598-z409-z245-z259-z140-z036-z566-z537-z614-z138-z616-z127-z428-z118-z522-z486-z054-z608-z596-z241-z239-z419
G032
z033-z378-z479-z146-z476-z608-z552-z585-z055-z575-z069-z389-z420-z383-z339-z274-z518-z496-z473
比如说我的txt文档里有不同线路的公交车经过的站点,用matlab导入txt文档,并求各个站点的邻接矩阵
G002
z107-z552-z477-z478-z480-z190-z527-z398-z253-z251-z114-z589-z332-z329-z183-z006-z040-z051-z224-z178-z434-z437-z462-z104-z455-z411-z624-z229-z384-z577-z283-z167-z217
G030
z068-z348-z598-z409-z245-z259-z140-z036-z566-z537-z614-z138-z616-z127-z428-z118-z522-z486-z054-z608-z596-z241-z239-z419
G032
z033-z378-z479-z146-z476-z608-z552-z585-z055-z575-z069-z389-z420-z383-z339-z274-z518-z496-z473
%几个前提:
1.G行后面就是车辆经过的站点信息
2.车辆的站点信息严格以“z数字-z数字-z数字”的形式存放
3.站点总数,我以车辆经过的站点信息中数字最大的为主.也意味着站点信息必须由1开始,并且连续到最大值.
%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Author :Wacs5
%Date :20091229
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
fid=fopen('bus.txt','r');
MaxStation=0; %站点总数(这里以出现的最大数字为主)
car=0; %多少路车
while(~feof(fid))
fline=fgetl(fid);
if (isempty(fline))%空行
continue;
end
if (fline(1)=='G') %车号行
car=car+1;
CarNum(car,1)=str2num(fline(2:end)); %记录车号
fline=fgetl(fid); %读取车辆经过的站点信息
temp=sscanf(fline,'z%d-'); %读取站点信息
CarInfo{car}=temp(:)'; %存至细胞元数组中
MaxStation=max(MaxStation,max(temp)); %更新站点总数
end
end
fclose(fid);
%%%%%文件读取完毕%%%%%
NeiMatr=zeros(MaxStation);
for i=1:car
for j=1:length(CarInfo{i})-1
NeiMatr(CarInfo{i}(j),CarInfo{i}(j+1))=1;
NeiMatr(CarInfo{i}(j+1),CarInfo{i}(j))=1;
end
end
1.G行后面就是车辆经过的站点信息
2.车辆的站点信息严格以“z数字-z数字-z数字”的形式存放
3.站点总数,我以车辆经过的站点信息中数字最大的为主.也意味着站点信息必须由1开始,并且连续到最大值.
%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Author :Wacs5
%Date :20091229
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
fid=fopen('bus.txt','r');
MaxStation=0; %站点总数(这里以出现的最大数字为主)
car=0; %多少路车
while(~feof(fid))
fline=fgetl(fid);
if (isempty(fline))%空行
continue;
end
if (fline(1)=='G') %车号行
car=car+1;
CarNum(car,1)=str2num(fline(2:end)); %记录车号
fline=fgetl(fid); %读取车辆经过的站点信息
temp=sscanf(fline,'z%d-'); %读取站点信息
CarInfo{car}=temp(:)'; %存至细胞元数组中
MaxStation=max(MaxStation,max(temp)); %更新站点总数
end
end
fclose(fid);
%%%%%文件读取完毕%%%%%
NeiMatr=zeros(MaxStation);
for i=1:car
for j=1:length(CarInfo{i})-1
NeiMatr(CarInfo{i}(j),CarInfo{i}(j+1))=1;
NeiMatr(CarInfo{i}(j+1),CarInfo{i}(j))=1;
end
end