# Finding neighbours in a matrix

190 views (last 30 days)
AelinAG on 11 Sep 2018
Edited: Andrei Bobrov on 12 Sep 2018
Hi,
If I have an m x n matrix, how can I print a list of all the possible neighbours(horizontally, vertically and diagonally)? For example matrix(1, 1) should only give 3 values, while an element somewhere in the centre of the matrix should give 8 values. I know there are questions on this website resembling mine, but since I'm really new to matlab, I wondered if there are any easier solutions(I don't mind if they're longer than they'd need to be) Thank you very much!

#### 1 Comment

Steven Lord on 11 Sep 2018
I'm guessing you're not trying to find the neighbors simply to have a list of the neighbors. If you explain how you plan to use this information, what you want to obtain at the end, we may be able to offer more targeted suggestions for how to achieve your end goal.
For instance, since you said you're new to MATLAB, one reason you might want to do this is you've been given a homework assignment to program Conway's Game of Life. If so, after you've written up your version you might be interested in reading the chapter on the Game of Life in Cleve Moler's Experiments with MATLAB.

KALYAN ACHARJYA on 11 Sep 2018
You can do it one by one, I don't think there is any direct command for that.
Suppose the present matrix position is i,j, matrix represent the given matrix.
%Here i,j represents the rows and colm value of the matrix
el_1=matrix(i,j) % Give the value of present element having position i,j
el_2=matrix(i,j-1) % Left element
el_3=matrix(i,j+1) % Right element
el_4=matrix(i-1,j) % for upper element
el_5=matrix(i+1,j)%Upper element
el_6=matrix(i-1,j-1) % West-North Diagonal Element....
.........
You can do for the rest from the following figure
Maybe indexing helps to reduce the lines, hope some expert will show the path in that direction.

AelinAG on 11 Sep 2018
What if my (x,y) is in a corner, how do I prevent Matlab telling me indices don't exist?
Image Analyst on 11 Sep 2018
blockproc() won't do that.

Image Analyst on 11 Sep 2018
You can use blockproc(). It can do that. See attached test.m demo. If you don't want to have it "jump" by 3 pixels every time, but want it to move over by every time, you have to overlap the windows like I do in the other attached demo. See if you can adapt it, if you need that (being a smart engineer, I'm sure you can).

Andrei Bobrov on 11 Sep 2018
Let a - your double array.
m = size(a)+2;
b = nan(m);
b(2:end-1,2:end-1) = a;
iii = reshape(1:numel(b),m);
ii = reshape(iii(~isnan(b)),1,1,[]);
out = reshape(b(ones(3).*ii + (-m(1):m(1):m(1)) + (-1:1)'),9,[]);

Image Analyst on 11 Sep 2018
So if
a =
11 81 21 26 63 52 45
28 44 95 15 50 51 10
30 56 17 66 13 72 45
72 6 29 60 86 67 37
32 44 90 30 44 2 45
26 34 39 35 13 73 35
Then the first output should be [81,44,28] (the values of the 3 neighbors) and the second should be (if you move over one) [21,95,44,28,11] (the values of the 5 neighbors). Your code gives
out =
Columns 1 through 20
NaN NaN NaN NaN NaN NaN NaN 11 28 30 72 32 NaN 81 44 56 6 44 NaN 21
NaN NaN NaN NaN NaN NaN 11 28 30 72 32 26 81 44 56 6 44 34 21 95
NaN NaN NaN NaN NaN NaN 28 30 72 32 26 NaN 44 56 6 44 34 NaN 95 17
NaN 11 28 30 72 32 NaN 81 44 56 6 44 NaN 21 95 17 29 90 NaN 26
11 28 30 72 32 26 81 44 56 6 44 34 21 95 17 29 90 39 26 15
28 30 72 32 26 NaN 44 56 6 44 34 NaN 95 17 29 90 39 NaN 15 66
NaN 81 44 56 6 44 NaN 21 95 17 29 90 NaN 26 15 66 60 30 NaN 63
81 44 56 6 44 34 21 95 17 29 90 39 26 15 66 60 30 35 63 50
44 56 6 44 34 NaN 95 17 29 90 39 NaN 15 66 60 30 35 NaN 50 13
Columns 21 through 40
95 17 29 90 NaN 26 15 66 60 30 NaN 63 50 13 86 44 NaN 52 51 72
17 29 90 39 26 15 66 60 30 35 63 50 13 86 44 13 52 51 72 67
29 90 39 NaN 15 66 60 30 35 NaN 50 13 86 44 13 NaN 51 72 67 2
15 66 60 30 NaN 63 50 13 86 44 NaN 52 51 72 67 2 NaN 45 10 45
66 60 30 35 63 50 13 86 44 13 52 51 72 67 2 73 45 10 45 37
60 30 35 NaN 50 13 86 44 13 NaN 51 72 67 2 73 NaN 10 45 37 45
50 13 86 44 NaN 52 51 72 67 2 NaN 45 10 45 37 45 NaN NaN NaN NaN
13 86 44 13 52 51 72 67 2 73 45 10 45 37 45 35 NaN NaN NaN NaN
86 44 13 NaN 51 72 67 2 73 NaN 10 45 37 45 35 NaN NaN NaN NaN NaN
Columns 41 through 42
67 2
2 73
73 NaN
37 45
45 35
35 NaN
NaN NaN
NaN NaN
NaN NaN
Not sure I understand how to interpret that.
Andrei Bobrov on 12 Sep 2018
interpretation of my result out:
We consider the matrix b-matrix a with boundary from nan elements.
The number of columns of the matrix out is equal to the number of elements of the matrix a.
Each column of the matrix out is a reshape of small region (3 by 3) of the matrix b.
b(1:3,1:3) -> out(:,1)
NaN NaN NaN NaN
NaN 11 81 NaN
NaN 28 44 -> NaN
NaN
11
28
NaN
81
44
b(2:4,1:3) -> out(:,2)
NaN 11 81 NaN
NaN 28 44 NaN
NaN 30 56 -> NaN
11
28
30
81
44
56
and so on.