MATLAB: Neighbor searching

search neighbor matrix

Hello!
I have a meshgrid created like this:
x=1:10; y=1:10; [X Y]=meshgrid(x,y);
X =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
and
Y =
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
I would like to implement a neighbor searching as follow: – select one position (X=4,Y=5) – obtain as output the coordinates of the points close to the previous one; in the case of a matrix (for example) 1X1 this would be: 1) X=3 Y=4 2) X=4 Y=4 3) X=5 Y=4 4) X=3 Y=5 5) X=5 Y=5 6) X=3 Y=6 7) X=4 Y=6 8) X=5 Y=6
Is there any function that does that in matlab ?

Best Answer

I = zeros(10);
x = 4;
y = 5;
I(x,y) = 1;
[yout,xout] = find((bwdist(I,'chessboard') == 1)');
variant 2
x=1:10;
y=1:10;
[X Y]=meshgrid(x,y);
i1 = 4;
j1 = 5;
[yout,xout] = find(max(abs(X-i1),abs(Y-j1))' == 1);