Skip to content
Advertisement

grid with circular distances python

I have a 2d grid of dim q(rows) p(columns). The element of the grid are the indices of the element itself. So the element a[i,j]=(i,j).

Now I need to create pair-wise distances between these points with circularity constraint, meaning that the element distance(a[i,p-1],a[i,0])=1 (I am using 0-index based matrix as in Python). Analogously distance(a[q-1,j],a[0,j])=1

Observe that distance(a[q-2,j],a[0,j]) is the shorter vertical path going from 0 to q-2 and the path from q2 to 0 (leveraging the circularity of the grid). Same thing happens horizontally.

My question is: is there a NumPy function that enables to quickly calculate such a pairwise distances matrix?

Advertisement

Answer

I don’t know of a function do do this, but it’s pretty easy to compute manually:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement