-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathloadAdjMatrixFull.m
79 lines (66 loc) · 2.2 KB
/
loadAdjMatrixFull.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
% loadAdjMatrix -- Compute adjacency matrix for given input image size
% Paras:
% @nRows : Number of rows for input image.
% @nCols : Number of columns for input image.
% Author: Xin Yi ([email protected])
% Date : 02/01/2016
function [adj, nNodesAll] = loadAdjMatrixFull(nRows, nCols)
nNodes = nRows*nCols;
nNodesAll = 3*nNodes;
y_ind = []; x_ind = [];
%% Add Down edge
% layer 1
ind = 1:nNodes;
exclude = sub2ind([nRows nCols],repmat(nRows,[1 nCols]),1:nCols);
ind = setdiff(ind,exclude);
y_ind = [y_ind, ind]; x_ind = [x_ind, ind+1];
% layer 2
ind = 1:nNodes;
exclude = sub2ind([nRows nCols],repmat(nRows,[1 nCols]),1:nCols);
ind = setdiff(ind,exclude);
ind = ind + nNodes;
y_ind = [y_ind, ind]; x_ind = [x_ind, ind+1];
% layer 3
ind = 1:nNodes;
exclude = sub2ind([nRows nCols],repmat(nRows,[1 nCols]),1:nCols);
ind = setdiff(ind,exclude);
ind = ind + 2*nNodes;
y_ind = [y_ind, ind]; x_ind = [x_ind, ind+1];
%% Add Right Edges
% layer 1
ind = 1:nNodes;
exclude = sub2ind([nRows nCols],1:nRows,repmat(nCols,[1 nRows]));
ind = setdiff(ind, exclude);
y_ind = [y_ind, ind]; x_ind = [x_ind, ind+nRows];
% layer 2
ind = 1:nNodes;
exclude = sub2ind([nRows nCols],1:nRows,repmat(nCols,[1 nRows]));
ind = setdiff(ind, exclude);
ind = ind + nNodes;
y_ind = [y_ind, ind]; x_ind = [x_ind, ind+nRows];
% layer 3
ind = 1:nNodes;
exclude = sub2ind([nRows nCols],1:nRows,repmat(nCols,[1 nRows]));
ind = setdiff(ind, exclude);
ind = ind + 2*nNodes;
y_ind = [y_ind, ind]; x_ind = [x_ind, ind+nRows];
%% Add Link From Layer 1 to Layer 2
y = 1:nRows; x = 1:nCols;
[y, x] = meshgrid(y, x);
ind1 = sub2ind([nRows nCols], y, x);
ind2 = sub2ind([nRows nCols], y, x) + nNodes;
y_ind = [y_ind, ind1(:)']; x_ind = [x_ind, ind2(:)'];
%% Add Link From Layer 2 to Layer 3
y = 1:nRows; x = 1:nCols;
[y, x] = meshgrid(y, x);
ind1 = sub2ind([nRows nCols], y, x) + nNodes;
ind2 = sub2ind([nRows nCols], y, x) + 2*nNodes;
y_ind = [y_ind, ind1(:)']; x_ind = [x_ind, ind2(:)'];
%% Add the reversed links
% adj = adj + adj';
y_ind_final = [y_ind, x_ind]; x_ind_final = [x_ind, y_ind];
%% Generate the sparse Matrix
edgeNum = length(y_ind_final);
edgeValue = ones(1, edgeNum);
adj = sparse(y_ind_final, x_ind_final, edgeValue, nNodesAll, nNodesAll);
end