【问题标题】:how to determine the neighbours of each node in a square graph ? MATLAB如何确定方图中每个节点的邻居? MATLAB
【发布时间】:2015-04-30 00:17:47
【问题描述】:

给定 n = 256 个节点的正方形表示,我希望将其显示在一个名为 neighbour{i} 的变量中,该变量返回每个节点的所有邻居。例如正方形中的节点数为 n= 256,所以我想使用 matlab 获取单元格数组中每个节点的邻居

for i=1:N
neighbour{i}=[neighbour{i} j]
end
                   ![%the code%
N = 16; M = 16;                  %# grid size
  CONNECTED = 8;                 %# 4-/8- connected points
%# which distance function
if CONNECTED == 4,     distFunc = 'cityblock';
elseif CONNECTED == 8, distFunc = 'chebychev'; end
%# compute adjacency matrix
\[X Y\] = meshgrid(1:N,1:M);
X = X(:); Y = Y(:);
adj = squareform( pdist(\[X Y\], distFunc) == 1 );
display(adj);
%# plot connected points on grid
\[xx yy\] = gplot(adj, \[X Y\]);
plot(xx, yy, 'ks-', 'MarkerFaceColor','r')
axis(\[0 N+1 0 M+1\])
\[X Y\] = meshgrid(1:N,1:M);
X = reshape(X',\[\],1) + 0.1; Y = reshape(Y',\[\],1) + 0.1;
text(X, Y(end:-1:1), cellstr(num2str((1:N*M)')) )
linked_node=cell(N,1);   
    % the most important step
  for X=1:N
      for Y=1:M
          if ((X~=Y) &&(squareform( pdist(\[X Y\], distFunc) == 1)))
               linked_node{X}= \[linked_node{X} Y\];
          end
      end
  end][1]

【问题讨论】:

  • 您的问题没有那么明确。如果我只是阅读您的问题,我会建议convolution。请参阅this answer 了解如何计算邻居数。但是,您的代码似乎考虑的远不止这些,而不仅仅是计算邻居。你能澄清一下你所说的邻居是什么意思吗?
  • deatr kkuilla;我的目标是为每个节点创建一个包含一组邻居的单元格数组,例如:如果节点 1 的邻居是:2 ,5 ,7 ,18 则 neighbours{1}= {2,5,7,18 }。 aformentionned 方形图是 n=256 个节点的图,因此我需要获取并显示:neighbours{1}= {2,5,7,18} neighbours{2}= {1,26,4,18}。 ......直到邻居{256}= {................}。 % 如果两个节点是链接的,则它们是邻居
  • 你有邻居名单吗?
  • 这是我需要构建的
  • 如何知道两个节点是否链接?

标签: matlab vector nodes


【解决方案1】:

其实答案是添加这个计算邻居的函数

function linked_node = find_neighbours(N, M, CONNECTED)

    %# which distance function
    if CONNECTED == 8
        distFunc = 'chebychev';
    else
        distFunc = 'cityblock';
    end

    linked_node=cell(N*M,1);   

      % the most important step
    for X=1:N
        for Y=1:M
            linked_node{sub2ind([N M], X,Y)} = [];
            if X - 1 > 0
                linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X-1,Y);
                if strcmp(distFunc, 'chebychev')
                    if Y - 1 > 0
                        linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X-1,Y-1);
                    end
                    if Y + 1 <= M
                        linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X-1,Y+1);
                    end
                end
            end
            if X + 1 <= N
                linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X+1,Y);
                if strcmp(distFunc, 'chebychev')
                    if Y - 1 > 0
                        linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X+1,Y-1);
                    end
                    if Y + 1 <= M
                        linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X+1,Y+1);
                    end
                end
            end
            if Y - 1 > 0
                linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X,Y-1);
            end
            if Y + 1 <= M
                linked_node{sub2ind([N M], X,Y)}(end+1) = sub2ind([N M], X,Y+1);
            end
        end
    end

    end

【讨论】:

    【解决方案2】:

    给定一个邻接矩阵:

    adj = [0 1 1 0 0;
           1 0 1 0 1;
           1 1 0 1 0;
           0 0 1 0 1;
           0 1 0 1 0];
    

    您可以将矩阵转换为包含在元胞数组中的邻接列表,如下所示:

    %// Put matrix into cell array by rows
    neighbors=mat2cell(adj,ones(1,size(adj,1)));
    %// Convert logical row to adjacent indices
    %// List sizes will vary, so 'UniformOutput' is false
    neighbors=cellfun(@find,neighbors,'UniformOutput',false);
    

    结果:

    neighbors{2}
    ans =
    
       1   3   5
    
    neighbors
    neighbors = 
    {
      [1,1] =
    
         2   3
    
      [2,1] =
    
         1   3   5
    
      [3,1] =
    
         1   2   4
    
      [4,1] =
    
         3   5
    
      [5,1] =
    
         2   4
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多