How to draw this tree as a lattice structure in Matlab?

The structure I want to draw should be the same as the one shown. Thank!enter image description here

+3
source share
3 answers

Here, a small function that I cracked will take as an input square, an upper triangular matrix and build a lattice structure, as described above:

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

And here is a test with a matrix of samples:

>> 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);

enter image description here

+4
source

I modified the code a bit so that it can print nodes, as well as several values ​​in each node.

Now A is a three-dimensional matrix and cannot take also empty values ​​(NaN), which are not printed in the tree

Ofc code is not optimal ... perhaps one of you can improve it

%# Compute all the coordinates needed for the lines and points:
      close all
      [N,L] = size(A);
      L=L/N;

      [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))));
      for i=1:L
        values(:,i) = strtrim(cellstr(num2str(A((i-1)*N*N+index))));
      end

      values = strrep(values, 'NaN', '   ');

    for i=1:N
        for j=i:N
            if i==1 && j==1
                nodes(i,j)=cellstr(strcat('N_','0'));
            else
                nodes(i,j)=cellstr(strcat('N_','{',repmat('u',1,(j-1)-(i-1)),repmat('d',1,(i-1)),'}'));
            end
        end
    end
    nodes = nodes(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',60,'MarkerEdgeColor','k','LineWidth',2);
      for i=1:L
        text(xPoints,yPoints+L*0.05-(i-1)*0.1,values(:,i),'Parent',hAxes,'HorizontalAlignment','center');
      end
      text(xPoints-0.4,yPoints,nodes,'Parent',hAxes,'HorizontalAlignment','center');
      hold off;
+2
source

I would use matlab to create a text file for use in Graphviz.

+1
source

Source: https://habr.com/ru/post/1796581/


All Articles