comparison toolboxes/FullBNT-1.0.7/graph/mk_2D_lattice_slow.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function G = mk_2D_lattice_slow(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 if nrows==1
56 G = zeros(1, ncols);
57 for i=1:ncols-1
58 G(i,i+1) = 1;
59 G(i+1,i) = 1;
60 end
61 if wrap_around
62 G(1,ncols) = 1;
63 G(ncols,1) = 1;
64 end
65 return;
66 end
67
68
69 npixels = nrows*ncols;
70
71 N = 1; E = 2; S = 3; W = 4;
72 if wrap_around
73 rows{N} = [nrows 1:nrows-1]; cols{N} = 1:ncols;
74 rows{E} = 1:nrows; cols{E} = [2:ncols 1];
75 rows{S} = [2:nrows 1]; cols{S} = 1:ncols;
76 rows{W} = 1:nrows; cols{W} = [ncols 1:ncols-1];
77 else
78 rows{N} = [1 1:nrows-1]; cols{N} = 1:ncols;
79 rows{E} = 1:nrows; cols{E} = [1 1:ncols-1];
80 rows{S} = [2:nrows nrows]; cols{S} = 1:ncols;
81 rows{W} = 1:nrows; cols{W} = [2:ncols ncols];
82 end
83
84 M = reshape(1:npixels, [nrows ncols]);
85 nbrs = cell(1, 4);
86 for i=1:4
87 nbrs{i} = M(rows{i}, cols{i});
88 end
89
90
91 G = zeros(npixels, npixels);
92 if wrap_around
93 for i=1:4
94 if 0
95 % naive
96 for p=1:npixels
97 G(p, nbrs{i}(p)) = 1;
98 end
99 else
100 % vectorized
101 ndx2 = sub2ind([npixels npixels], 1:npixels, nbrs{i}(:)');
102 G(ndx2) = 1;
103 end
104 end
105 else
106 i = N;
107 mask = ones(nrows, ncols);
108 mask(1,:) = 0; % pixels in row 1 have no nbr to the north
109 ndx = find(mask);
110 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
111 G(ndx2) = 1;
112
113 i = E;
114 mask = ones(nrows, ncols);
115 mask(:,ncols) = 0;
116 ndx = find(mask);
117 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
118 G(ndx2) = 1;
119
120 i = S;
121 mask = ones(nrows, ncols);
122 mask(nrows,:)=0;
123 ndx = find(mask);
124 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
125 G(ndx2) = 1;
126
127 i = W;
128 mask = ones(nrows, ncols);
129 mask(:,1)=0;
130 ndx = find(mask);
131 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
132 G(ndx2) = 1;
133 end
134
135 G = setdiag(G, 0);