当求出二维矩阵m以后可以借助Matlab绘图更为生动的树形图,如下面的代码所示,将m矩阵赋值给mx,利用下面的程序可以得到比较不错的效果图:
- clear all;
- clc;
- close all;
-
- mx =...
- [0 0 0 1 0 0 0
- 0 1 0 0 0 1 0
- 1 0 1 0 1 0 0];
-
- cnt = 0;
-
- clear mx;
-
- mx = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0];
- [row,col] = size(mx);
- for i = 1:row
- for j = 1:col
- hold on;
- if (mx(i,j) == 1)
- plot(j,row-i,'Marker','.','Markersize',30,'MarkerFaceColor','k','MarkerEdgeColor','k');
- text(j,row-i-0.05*row,char('A'+cnt));
- cnt = cnt + 1;
- end
- end
- end
-
- ylim([-1,row]);
- grid on;
-
- d = col + 1;
- for i = 1:row-1
- % for j = 1:col
- % if (mx(i,j) == 1)
- % d = j;
- % break;
- % end
- % end
- d = d / 2;
- for j = 1:col
- if (mx(i,j) == 1)
- for k = 1:col
- if ((mx(i+1,k) == 1) & (abs(k - j) <= floor(d/2)))
- hold on;
- plot([j,k],[row-i,row-(i+1)],'-k','Linewidth',2.5);
- end
- end
- end
- end
- end