annotate toolboxes/FullBNT-1.0.7/graph/mk_2D_lattice_slow.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function G = mk_2D_lattice_slow(nrows, ncols, wrap_around)
Daniel@0 2 % MK_2D_LATTICE Return adjacency matrix for 4-nearest neighbor connected 2D lattice
Daniel@0 3 % G = mk_2D_lattice(nrows, ncols, wrap_around)
Daniel@0 4 % G(k1, k2) = 1 iff k1=(i1,j1) is connected to k2=(i2,j2)
Daniel@0 5 %
Daniel@0 6 % If wrap_around = 1, we use toroidal boundary conditions (default = 0)
Daniel@0 7 %
Daniel@0 8 % Nodes are assumed numbered as in the following 3x3 lattice
Daniel@0 9 % 1 4 7
Daniel@0 10 % 2 5 8
Daniel@0 11 % 3 6 9
Daniel@0 12 %
Daniel@0 13 % e.g., G = mk_2D_lattice(3, 3, 0) returns
Daniel@0 14 % 0 1 0 1 0 0 0 0 0
Daniel@0 15 % 1 0 1 0 1 0 0 0 0
Daniel@0 16 % 0 1 0 0 0 1 0 0 0
Daniel@0 17 % 1 0 0 0 1 0 1 0 0
Daniel@0 18 % 0 1 0 1 0 1 0 1 0
Daniel@0 19 % 0 0 1 0 1 0 0 0 1
Daniel@0 20 % 0 0 0 1 0 0 0 1 0
Daniel@0 21 % 0 0 0 0 1 0 1 0 1
Daniel@0 22 % 0 0 0 0 0 1 0 1 0
Daniel@0 23 % so find(G(5,:)) = [2 4 6 8]
Daniel@0 24 % but find(G(1,:)) = [2 4]
Daniel@0 25 %
Daniel@0 26 % Using wrap around, G = mk_2D_lattice(3, 3, 1), we get
Daniel@0 27 % 0 1 1 1 0 0 1 0 0
Daniel@0 28 % 1 0 1 0 1 0 0 1 0
Daniel@0 29 % 1 1 0 0 0 1 0 0 1
Daniel@0 30 % 1 0 0 0 1 1 1 0 0
Daniel@0 31 % 0 1 0 1 0 1 0 1 0
Daniel@0 32 % 0 0 1 1 1 0 0 0 1
Daniel@0 33 % 1 0 0 1 0 0 0 1 1
Daniel@0 34 % 0 1 0 0 1 0 1 0 1
Daniel@0 35 % 0 0 1 0 0 1 1 1 0
Daniel@0 36 % so find(G(5,:)) = [2 4 6 8]
Daniel@0 37 % and find(G(1,:)) = [2 3 4 7]
Daniel@0 38
Daniel@0 39 if nargin < 3, wrap_around = 0; end
Daniel@0 40
Daniel@0 41 % M contains the number of each cell e.g.
Daniel@0 42 % 1 4 7
Daniel@0 43 % 2 5 8
Daniel@0 44 % 3 6 9
Daniel@0 45 % North neighbors (assuming wrap around) are
Daniel@0 46 % 3 6 9
Daniel@0 47 % 1 4 7
Daniel@0 48 % 2 5 8
Daniel@0 49 % Without wrap around, they are
Daniel@0 50 % 1 4 7
Daniel@0 51 % 1 4 7
Daniel@0 52 % 2 5 8
Daniel@0 53 % The first row is arbitrary, since pixels at the top have no north neighbor.
Daniel@0 54
Daniel@0 55 if nrows==1
Daniel@0 56 G = zeros(1, ncols);
Daniel@0 57 for i=1:ncols-1
Daniel@0 58 G(i,i+1) = 1;
Daniel@0 59 G(i+1,i) = 1;
Daniel@0 60 end
Daniel@0 61 if wrap_around
Daniel@0 62 G(1,ncols) = 1;
Daniel@0 63 G(ncols,1) = 1;
Daniel@0 64 end
Daniel@0 65 return;
Daniel@0 66 end
Daniel@0 67
Daniel@0 68
Daniel@0 69 npixels = nrows*ncols;
Daniel@0 70
Daniel@0 71 N = 1; E = 2; S = 3; W = 4;
Daniel@0 72 if wrap_around
Daniel@0 73 rows{N} = [nrows 1:nrows-1]; cols{N} = 1:ncols;
Daniel@0 74 rows{E} = 1:nrows; cols{E} = [2:ncols 1];
Daniel@0 75 rows{S} = [2:nrows 1]; cols{S} = 1:ncols;
Daniel@0 76 rows{W} = 1:nrows; cols{W} = [ncols 1:ncols-1];
Daniel@0 77 else
Daniel@0 78 rows{N} = [1 1:nrows-1]; cols{N} = 1:ncols;
Daniel@0 79 rows{E} = 1:nrows; cols{E} = [1 1:ncols-1];
Daniel@0 80 rows{S} = [2:nrows nrows]; cols{S} = 1:ncols;
Daniel@0 81 rows{W} = 1:nrows; cols{W} = [2:ncols ncols];
Daniel@0 82 end
Daniel@0 83
Daniel@0 84 M = reshape(1:npixels, [nrows ncols]);
Daniel@0 85 nbrs = cell(1, 4);
Daniel@0 86 for i=1:4
Daniel@0 87 nbrs{i} = M(rows{i}, cols{i});
Daniel@0 88 end
Daniel@0 89
Daniel@0 90
Daniel@0 91 G = zeros(npixels, npixels);
Daniel@0 92 if wrap_around
Daniel@0 93 for i=1:4
Daniel@0 94 if 0
Daniel@0 95 % naive
Daniel@0 96 for p=1:npixels
Daniel@0 97 G(p, nbrs{i}(p)) = 1;
Daniel@0 98 end
Daniel@0 99 else
Daniel@0 100 % vectorized
Daniel@0 101 ndx2 = sub2ind([npixels npixels], 1:npixels, nbrs{i}(:)');
Daniel@0 102 G(ndx2) = 1;
Daniel@0 103 end
Daniel@0 104 end
Daniel@0 105 else
Daniel@0 106 i = N;
Daniel@0 107 mask = ones(nrows, ncols);
Daniel@0 108 mask(1,:) = 0; % pixels in row 1 have no nbr to the north
Daniel@0 109 ndx = find(mask);
Daniel@0 110 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
Daniel@0 111 G(ndx2) = 1;
Daniel@0 112
Daniel@0 113 i = E;
Daniel@0 114 mask = ones(nrows, ncols);
Daniel@0 115 mask(:,ncols) = 0;
Daniel@0 116 ndx = find(mask);
Daniel@0 117 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
Daniel@0 118 G(ndx2) = 1;
Daniel@0 119
Daniel@0 120 i = S;
Daniel@0 121 mask = ones(nrows, ncols);
Daniel@0 122 mask(nrows,:)=0;
Daniel@0 123 ndx = find(mask);
Daniel@0 124 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
Daniel@0 125 G(ndx2) = 1;
Daniel@0 126
Daniel@0 127 i = W;
Daniel@0 128 mask = ones(nrows, ncols);
Daniel@0 129 mask(:,1)=0;
Daniel@0 130 ndx = find(mask);
Daniel@0 131 ndx2 = sub2ind([npixels npixels], ndx, nbrs{i}(ndx));
Daniel@0 132 G(ndx2) = 1;
Daniel@0 133 end
Daniel@0 134
Daniel@0 135 G = setdiag(G, 0);