matlab画一个树,如何在Matlab中像格子结构一样绘制这棵树?

这是一个我掀起的一个小函数,它将输入一个正方形的上三角矩阵并绘制如上所述的晶格结构:

function hFigure = plot_lattice(A)

%# Compute all the coordinates needed for the lines and points:

N = size(A,1);

[xPoints,yPoints] = meshgrid(0:N-1);

yPoints = bsxfun(@plus,-yPoints,0:0.5:(N-0.5)/2);

xLines = [xPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); ...

xPoints([1:N-1 N:-1:2],N).']; %'

yLines = [yPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); ...

yPoints([1:N-1 N:-1:2],N).']; %'

index = find(triu(reshape(1:N^2,N,N)));

xPoints = xPoints(index);

yPoints = yPoints(index);

values = strtrim(cellstr(num2str(A(index))));

%# Create the figure:

hFigure = figure('Color','w');

hAxes = axes('Parent',hFigure,'XLim',[-0.5 N-0.5],...

'YLim',[min(yPoints)-0.5 max(yPoints)+0.5],...

'YColor','w','XTick',0:N-1,'LineWidth',2);

hold on;

plot(hAxes,xLines,yLines,'k','LineWidth',2);

plot(hAxes,xPoints,yPoints,'o','MarkerFaceColor',[0.96 0.96 0.86],...

'MarkerSize',30,'MarkerEdgeColor','k','LineWidth',2);

text(xPoints,yPoints,values,'Parent',hAxes,...

'HorizontalAlignment','center');

hold off;

end

这是一个带有样本矩阵的测试:

>> A = triu(reshape(1:25,5,5))

A =

1 6 11 16 21

0 7 12 17 22

0 0 13 18 23

0 0 0 19 24

0 0 0 0 25

>> plot_lattice(A);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值