-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighs.m
190 lines (176 loc) · 8.67 KB
/
neighs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
% neighs.m
% Copyright (c) Prasanth "Prash" Ganesan
% Author email: <prasganesan.pg@gmail.com>
% This code is a naive way to find the neighbor elements of a given location of a matrix
function [neighbors, inds] = neighs(row, col, Areaa)
if nargout == 1 && nargin <= 3
error('Error in funtion neighs : Not enough inputs');
end
if nargin < 2
error('Error in funtion neighs : Not enough inputs');
elseif nargin == 3
neighbors = [];
inds = {};
if row ~= 1 && row ~= size(Areaa,1) && col ~= 1 && col ~= size(Areaa,2)
neighbors(1) = Areaa(row-1,col-1); % Upper left.
neighbors(2) = Areaa(row-1,col); % Upper middle.
neighbors(3) = Areaa(row-1,col+1); % Upper right.
neighbors(4) = Areaa(row,col-1); % left.
neighbors(5) = Areaa(row,col+1); % right.
neighbors(6) = Areaa(row+1,col+1); % Lower right.
neighbors(7) = Areaa(row+1,col); % lower middle.
neighbors(8) = Areaa(row+1,col-1); % Lower left.
end
if row == 1 && col == 1
neighbors(1) = Areaa(row,col+1); % right.
neighbors(2) = Areaa(row+1,col); % lower middle.
neighbors(3) = Areaa(row+1,col+1); % Lower right.
elseif row == size(Areaa,1) && col == 1
neighbors(1) = Areaa(row-1,col); % Upper middle.
neighbors(2) = Areaa(row,col+1); % right.
neighbors(3) = Areaa(row-1,col+1); % Upper right.
elseif row == 1 && col == size(Areaa,2)
neighbors(1) = Areaa(row,col-1); % left.
neighbors(2) = Areaa(row+1,col); % lower middle.
neighbors(3) = Areaa(row+1,col-1); % Lower left.
elseif row == size(Areaa,1) && col == size(Areaa,2)
neighbors(1) = Areaa(row,col-1); % left.
neighbors(2) = Areaa(row-1,col); % Upper middle.
neighbors(3) = Areaa(row-1,col-1); % Upper left.
end
if row ~= 1 && row ~= size(Areaa,1) && col == 1
neighbors(1) = Areaa(row-1,col); % Upper middle.
neighbors(2) = Areaa(row-1,col+1); % Upper right.
neighbors(3) = Areaa(row,col+1); % right.
neighbors(4) = Areaa(row+1,col+1); % Lower right.
neighbors(5) = Areaa(row+1,col); % lower middle.
elseif row ~= 1 && row ~= size(Areaa,1) && col == size(Areaa,2)
neighbors(1) = Areaa(row-1,col-1); % Upper left.
neighbors(2) = Areaa(row-1,col); % Upper middle.
neighbors(3) = Areaa(row+1,col); % lower middle.
neighbors(4) = Areaa(row+1,col-1); % Lower left.
neighbors(5) = Areaa(row,col-1); % left.
elseif col ~= 1 && col ~= size(Areaa,2) && row == 1
neighbors(1) = Areaa(row+1,col+1); % Lower right.
neighbors(2) = Areaa(row+1,col); % lower middle.
neighbors(3) = Areaa(row+1,col-1); % Lower left.
neighbors(4) = Areaa(row,col-1); % left.
neighbors(5) = Areaa(row,col+1); % right.
elseif col ~= 1 && col ~= size(Areaa,2) && row == size(Areaa,1)
neighbors(1) = Areaa(row-1,col-1); % Upper left.
neighbors(2) = Areaa(row-1,col); % Upper middle.
neighbors(3) = Areaa(row-1,col+1); % Upper right.
neighbors(4) = Areaa(row,col-1); % left.
neighbors(5) = Areaa(row,col+1); % right.
end
% Indices
if row ~= 1 && row ~= size(Areaa,1) && col ~= 1 && col ~= size(Areaa,2)
inds{1} = [row-1,col-1]; % Upper left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row-1,col+1]; % Upper right.
inds{4} = [row,col-1]; % left.
inds{5} = [row,col+1]; % right.
inds{6} = [row+1,col+1]; % Lower right.
inds{7} = [row+1,col]; % lower middle.
inds{8} = [row+1,col-1]; % Lower left.
end
if row == 1 && col == 1
inds{1} = [row,col+1]; % right.
inds{2} = [row+1,col]; % lower middle.
inds{3} = [row+1,col+1]; % Lower right.
elseif row == size(Areaa,1) && col == 1
inds{1} = [row-1,col]; % Upper middle.
inds{2} = [row,col+1]; % right.
inds{3} = [row-1,col+1]; % Upper right.
elseif row == 1 && col == size(Areaa,2)
inds{1} = [row,col-1]; % left.
inds{2} = [row+1,col]; % lower middle.
inds{3} = [row+1,col-1]; % Lower left.
elseif row == size(Areaa,1) && col == size(Areaa,2)
inds{1} = [row,col-1]; % left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row-1,col-1]; % Upper left.
end
if row ~= 1 && row ~= size(Areaa,1) && col == 1
inds{1} = [row-1,col]; % Upper middle.
inds{2} = [row-1,col+1]; % Upper right.
inds{3} = [row,col+1]; % right.
inds{4} = [row+1,col+1]; % Lower right.
inds{5} = [row+1,col]; % lower middle.
elseif row ~= 1 && row ~= size(Areaa,1) && col == size(Areaa,2)
inds{1} = [row-1,col-1]; % Upper left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row+1,col]; % lower middle.
inds{4} = [row+1,col-1]; % Lower left.
inds{5} = [row,col-1]; % left.
elseif col ~= 1 && col ~= size(Areaa,2) && row == 1
inds{1} = [row+1,col+1]; % Lower right.
inds{2} = [row+1,col]; % lower middle.
inds{3} = [row+1,col-1]; % Lower left.
inds{4} = [row,col-1]; % left.
inds{5} = [row,col+1]; % right.
elseif col ~= 1 && col ~= size(Areaa,2) && row == size(Areaa,1)
inds{1} = [row-1,col-1]; % Upper left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row-1,col+1]; % Upper right.
inds{4} = [row,col-1]; % left.
inds{5} = [row,col+1]; % right.
end
else
neighbors = [];
inds = {};
% Indices
if row ~= 1 && row ~= size(Areaa,1) && col ~= 1 && col ~= size(Areaa,2)
inds{1} = [row-1,col-1]; % Upper left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row-1,col+1]; % Upper right.
inds{4} = [row,col-1]; % left.
inds{5} = [row,col+1]; % right.
inds{6} = [row+1,col+1]; % Lower right.
inds{7} = [row+1,col]; % lower middle.
inds{8} = [row+1,col-1]; % Lower left.
end
if row == 1 && col == 1
inds{1} = [row,col+1]; % right.
inds{2} = [row+1,col]; % lower middle.
inds{3} = [row+1,col+1]; % Lower right.
elseif row == size(Areaa,1) && col == 1
inds{1} = [row-1,col]; % Upper middle.
inds{2} = [row,col+1]; % right.
inds{3} = [row-1,col+1]; % Upper right.
elseif row == 1 && col == size(Areaa,2)
inds{1} = [row,col-1]; % left.
inds{2} = [row+1,col]; % lower middle.
inds{3} = [row+1,col-1]; % Lower left.
elseif row == size(Areaa,1) && col == size(Areaa,2)
inds{1} = [row,col-1]; % left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row-1,col-1]; % Upper left.
end
if row ~= 1 && row ~= size(Areaa,1) && col == 1
inds{1} = [row-1,col]; % Upper middle.
inds{2} = [row-1,col+1]; % Upper right.
inds{3} = [row,col+1]; % right.
inds{4} = [row+1,col+1]; % Lower right.
inds{5} = [row+1,col]; % lower middle.
elseif row ~= 1 && row ~= size(Areaa,1) && col == size(Areaa,2)
inds{1} = [row-1,col-1]; % Upper left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row+1,col]; % lower middle.
inds{4} = [row+1,col-1]; % Lower left.
inds{5} = [row,col-1]; % left.
elseif col ~= 1 && col ~= size(Areaa,2) && row == 1
inds{1} = [row+1,col+1]; % Lower right.
inds{2} = [row+1,col]; % lower middle.
inds{3} = [row+1,col-1]; % Lower left.
inds{4} = [row,col-1]; % left.
inds{5} = [row,col+1]; % right.
elseif col ~= 1 && col ~= size(Areaa,2) && row == size(Areaa,1)
inds{1} = [row-1,col-1]; % Upper left.
inds{2} = [row-1,col]; % Upper middle.
inds{3} = [row-1,col+1]; % Upper right.
inds{4} = [row,col-1]; % left.
inds{5} = [row,col+1]; % right.
end
end
end