Finding 4 closest points So I am trying to use bilinear interpolation to extrapolate data for certain values of a 3-dimensional array. But, the issue is that the bilinear interpolation requires an input of the 4 closest numbers by distance in order for the equations to be solved. Does anyone have an idea of how I can obtain these 4 values. I am going to put some context below for better understanding.
62  0   63  0
0   0   0   0
63  0   61  0
0   0   0   0

I am going to refer to the coortinates of the elements in the form of (row,column).
So for example, consider this array above. For context, I need a way to find the 4 closest non-zero points to (1,2). If I solved this manually, the points would be (1,1),(1,3),(3,1),(3,3).
This is only a small part of a very large array that I have given for context. If anyone can help me with the coding, it would be appreciated.
EDIT:  If you are looking for the four points in 3space with odd coordinates that are closest to $(x,y,z)$ they are the nearest point and three of its neighbours.
$$X=2floor(x/2)+1\\Y=2floor(y/2)+1\\Z=2floor(z/2)+1$$
The nearest point is $(X,Y,Z)$.
If $x\gt X$, another point is $(X+2,Y,Z)$, otherwise $(X-2,Y,Z)$
If $y \gt Y$, the third point is $(X,Y+2,Z)$ otherwise $(X,Y-2,Z)$
If $z\gt Z$, the fourth point is $(X,Y,Z+2)$ otherwise $(X,Y,Z-2)$
If you are looking for the coordinates of the square that contains a point $(x,y)$, where those coordinates are odd, let
$$X=2×floor\left(\frac{x+1}2\right)\\
Y=2×floor\left(\frac{y+1}2\right)$$
Then the coordinates are
$$(X+1,Y+1),(X+1,Y-1),(X-1,Y+1),(X-1,Y-1)$$
If you are close enough to one of the lattice points, then the four nearest don't form a square, but form a T instead.  Is that important ?
If the points are irregular, you might run through the list, keeping just the four nearest so far.  Then you need only run through the list once.
EDIT:  If you are looking for the four points in 3space with odd coordinates that are closest to $(x,y,z)$ they are the nearest point and three of its neighbours.
$$X=2floor(x/2)+1\\Y=2floor(y/2)+1\\Z=2floor(z/2)+1$$
The nearest point is $(X,Y,Z)$.
If $x\gt X$, another point is $(X+2,Y,Z)$, otherwise $(X-2,Y,Z)$
If $y \gt Y$, the third point is $(X,Y+2,Z)$ otherwise $(X,Y-2,Z)$
If $z\gt Z$, the fourth point is $(X,Y,Z+2)$ otherwise $(X,Y,Z-2)$
If you are looking for the coordinates of the square that contains a point $(x,y)$, where those coordinates are odd, let
$$X=2×floor\left(\frac{x+1}2\right)\\
Y=2×floor\left(\frac{y+1}2\right)$$
Then the coordinates are
$$(X+1,Y+1),(X+1,Y-1),(X-1,Y+1),(X-1,Y-1)$$
If you are close enough to one of the lattice points, then the four nearest don't form a square, but form a T instead.  Is that important ?
If the points are irregular, you might run through the list, keeping just the four nearest so far.  Then you need only run through the list once.