【Matlab】将图的矩阵转化成无向图

clear all;clc;
[A,Name,allData]=xlsread(Nametable,'knowing someones dialect'); %read excel
r_size=size(Name);
if r_size(1)~=r_size(2)  %判断是否为方阵
    disp('NOT A SQUARE MATRIX');
    return;
end
len=r_size(1);  
r=1.35  
w=floor(sqrt(len)); %使点的分布尽可能的随机避免重合点      
h=floor(len/w);
for i=1:h        
    for j=1:w
        pointx=98*rand(len,1)+(j-1)*19;
        pointy=97*rand(len,1)+(i-1)*13;
    end
end
pointx=floor(pointx);  
pointy=floor(pointy);
theta=0:pi/25:2*pi;  
[tempx,tempy]=pol2cart(theta',r); 
point=[pointx,pointy]; %50*2
hold on;  %保存axes内图像用的,如果你在新画图像之后不想覆盖原图像就要加上hold on这句话
for i=1:len
    temp=[tempx,tempy]+[point(i,1)*ones(length(tempx),1),point(i,2)*ones(length(tempx),1)]; 
    plot(temp(:,1),temp(:,2),'b'); 
    fill(temp(:,1),temp(:,2),'y');  
    text(point(i,1)-0.5,point(i,2),num2str(i),'FontName','黑体','FontWeight','Bold');  
    title('knowing someones dialect','Color','c'); 
end
for i=1:len
    for j=i:len
        if Name{i,j}=='y'
           Linkpoints(point(i,:),point(j,:),r); 
        end
    end
end
function Linkpoints(point1,point2,r)
%从节点的边发出连接两点
temp=point2-point1;
if (~temp(1))&&(~temp(2))
    return; 
end
theta=cart2pol(temp(1),temp(2));
[point1_x,point1_y]=pol2cart(theta,r);
point_1=[point1_x,point1_y]+point1;
[point2_x,point2_y]=pol2cart(theta+(2*(theta

你可能感兴趣的:(Matlab)