matlab找出无向图中路径_matlab无向图 有向图画法

这段代码定义了一个名为`graph`的MATLAB函数,用于根据邻接矩阵绘制无向图或有向图。函数接受邻接矩阵和控制参数作为输入,控制参数决定是否将图画为有向。当邻接矩阵不是方阵或者输入参数不足时,函数会给出错误提示。函数首先计算节点的位置,然后遍历矩阵,通过`link_plot`函数连接相邻的节点。最后,设置图形的范围并关闭坐标轴显示。
摘要由CSDN通过智能技术生成

function graph(rel,control)

%由邻接矩阵画图

%输入为邻接矩阵,必须为方阵;

%第二个输入为控制量,0表示无向图,1表示有向图。默认值为0

r_size=size(rel);%获取矩阵大小

if nargin<2 %如果参数小于2,默认无向图

control=0;

end

if r_size(1)~=r_size(2)

disp('Wrong Input! The input must be a square matrix!');%输入为邻接矩阵,必须为方阵

return;

end

len=r_size(2);

!echo len;

disp(len);

rho=10;%限制图尺寸的大小

r=1/1.05^len;%点的半径

theta=0:(2*pi/len):2*pi*(1-1/len);%以0开始,步长2*pi/len,结束2*pi*(1-1/len),相当于有len个点

[pointx,pointy]=pol2cart(theta',rho);

theta=0:pi/36:2*pi;%73个点

!echo theta:

disp(theta);

[tempx,tempy]=pol2cart(theta',r);%73个坐标

point=[pointx,pointy];%6个坐标点

!echo point;

disp(point);

hold on

for i=1:len

temp=[tempx,tempy]+[point(i,1)*ones(length(tempx),1),point(i,2)*ones(length(tempx),1)];%73行1列的1矩阵与point每个坐标相乘

plot(temp(:,1),temp(:,2),'r');%plot(x,y),画出圆圈

%plot(point(:,1),point(:,2),'r');%plot(x,y)

text(point(i,1)-0.3,point(i,2),num2str(i));

%画点

end

for i=1:len

for j=1:len

if rel(i,j)%如果邻接矩阵rel中该点是1,代表相连

link_plot(point(i,:),point(j,:),r,control);

%连接有关系的点

end

end

end

set(gca,'XLim',[-rho-r,rho+r],'YLim',[-rho-r,rho+r]);

axis off

%%

function link_plot(point1,point2,r,control)

%连接两点

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

在命令行:

A=ones(8);

graph(8)

313b464612491ba756c595a2eff88a38.png

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值