Below I give an example in 2D, but my real question would be regarding 3D (with or without periodic boundaries). Find all unique neighbors for each segment id using 8 neighbors (2d) or 26 neighbors (3d).
Given the following array:
INPUT
matrix=[
[1, 2, 2, 3],
[1, 4, 5, 3],
[4, 4, 5, 3]
]
OUTPUT non-periodic
1 : [1, 2, 4]
2 : [1, 2, 3, 4, 5]
3 : [2, 3, 5]
4 : [1, 2, 4, 5]
5 : [2, 3, 4, 5]
OUTPUT periodic
1 : [1, 2, 3, 4]
2 : [1, 2, 3, 4, 5]
3 : [1, 2, 3, 4, 5]
4 : [1, 2, 3, 4, 5]
5 : [2, 3, 4, 5]
I got a stack of for loops to do the job, but I would really like to use a more numpy/scipy based approach if that could work. I feel like some form of clever convolve could do the trick, I am just not seeing how.
An example of such slow code is presented here.