To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _FullBNT / BNT / graph / Old / .svn / text-base / mk_2D_lattice_slow.m.svn-base @ 8:b5b38998ef3b

History | View | Annotate | Download (2.9 KB)

1
function G = mk_2D_lattice(nrows, ncols, wrap_around) 
2
% MK_2D_LATTICE Return adjacency matrix for 4-nearest neighbor connected 2D lattice
3
% G = mk_2D_lattice(nrows, ncols, wrap_around)
4
% G(k1, k2) = 1 iff k1=(i1,j1) is connected to k2=(i2,j2)
5
%
6
% If wrap_around = 1, we use toroidal boundary conditions (default = 0)
7
%
8
% Nodes are assumed numbered as in the following 3x3 lattice
9
%   1 4 7
10
%   2 5 8 
11
%   3 6 9
12
%
13
% e.g., G = mk_2D_lattice(3, 3, 0) returns
14
%   0 1 0 1 0 0 0 0 0 
15
%   1 0 1 0 1 0 0 0 0 
16
%   0 1 0 0 0 1 0 0 0 
17
%   1 0 0 0 1 0 1 0 0 
18
%   0 1 0 1 0 1 0 1 0 
19
%   0 0 1 0 1 0 0 0 1 
20
%   0 0 0 1 0 0 0 1 0 
21
%   0 0 0 0 1 0 1 0 1 
22
%   0 0 0 0 0 1 0 1 0 
23
% so find(G(5,:)) = [2 4 6 8] 
24
% but find(G(1,:)) = [2 4]
25
%
26
% Using wrap around, G = mk_2D_lattice(3, 3, 1), we get
27
%   0 1 1 1 0 0 1 0 0 
28
%   1 0 1 0 1 0 0 1 0 
29
%   1 1 0 0 0 1 0 0 1 
30
%   1 0 0 0 1 1 1 0 0 
31
%   0 1 0 1 0 1 0 1 0 
32
%   0 0 1 1 1 0 0 0 1 
33
%   1 0 0 1 0 0 0 1 1 
34
%   0 1 0 0 1 0 1 0 1 
35
%   0 0 1 0 0 1 1 1 0 
36
% so find(G(5,:)) = [2 4 6 8] 
37
% and find(G(1,:)) = [2 3 4 7]
38

    
39
if nargin < 3, wrap_around = 0; end
40

    
41
% M contains the number of each cell e.g.
42
%   1 4 7
43
%   2 5 8 
44
%   3 6 9
45
% North neighbors (assuming wrap around) are
46
%   3 6 9
47
%   1 4 7
48
%   2 5 8
49
% Without wrap around, they are
50
%   1 4 7
51
%   1 4 7
52
%   2 5 8
53
% The first row is arbitrary, since pixels at the top have no north neighbor.
54

    
55
npixels = nrows*ncols;
56

    
57
N = 1; E = 2; S = 3; W = 4;
58
if wrap_around
59
  rows{N} = [nrows 1:nrows-1]; cols{N} = 1:ncols;
60
  rows{E} = 1:nrows; cols{E} = [2:ncols 1];
61
  rows{S} = [2:nrows 1]; cols{S} = 1:ncols;
62
  rows{W} = 1:nrows; cols{W} = [ncols 1:ncols-1];
63
else
64
  rows{N} = [1 1:nrows-1]; cols{N} = 1:ncols;
65
  rows{E} = 1:nrows; cols{E} = [2:ncols 2];
66
  rows{S} = [2:nrows 2]; cols{S} = 1:ncols;
67
  rows{W} = 1:nrows; cols{W} = [1 1:ncols-1];
68
end
69

    
70
M = reshape(1:npixels, [nrows ncols]);
71
nbrs = cell(1, 4);
72
for i=1:4
73
  nbrs{i} = M(rows{i}, cols{i});
74
end
75

    
76

    
77
G = zeros(npixels, npixels);
78
if wrap_around
79
  for i=1:4
80
    if 0
81
      % naive
82
      for p=1:npixels
83
	G(p, nbrs{i}(p)) = 1;
84
      end
85
    else
86
      % vectorized
87
      ndx2 = sub2ind([npixels npixels], 1:npixels, nbrs{i}(:)');
88
      G(ndx2) = 1;
89
    end
90
  end
91
else
92
  i = N;
93
  mask = ones(nrows, ncols);
94
  mask(1,:) = 0; % pixels in row 1 have no nbr to the north
95
  ndx = find(mask);
96
  ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
97
  G(ndx2) = 1;
98

    
99
  i = E;
100
  mask = ones(nrows, ncols);
101
  mask(:,ncols) = 0;
102
  ndx = find(mask);
103
  ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
104
  G(ndx2) = 1;
105

    
106
  i = S;
107
  mask = ones(nrows, ncols);
108
  mask(nrows,:)=0;
109
  ndx = find(mask);
110
  ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
111
  G(ndx2) = 1;
112
  
113
  i = W;
114
  mask = ones(nrows, ncols);
115
  mask(:,1)=0;
116
  ndx = find(mask);
117
  ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
118
  G(ndx2) = 1;
119
end
120

    
121
G = setdiag(G, 0);