如何确定方形图中每个节点的邻居? MATLAB

时间:2015-04-28 11:38:20

标签: matlab vector nodes

给定n = 256个节点的方形表示,我希望在名为neighbor {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]

2 个答案:

答案 0 :(得分:1)

实际上答案是添加这个计算neghbours的函数

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lib1">
    <uses-sdk android:minSdkVersion="4" />
</manifest> 

答案 1 :(得分:0)

给出邻接矩阵:

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

}