0

dist(n) generates a (n x n) matrix of elements. Each element is the straight-line "distance" from either element [0, 0] or the element just beyond another corner, whichever is closest.

I'm trying to roll the rows and columns to get an array with distances from the centre point, but I'm unsure about how to do this in Python.

In IDL:

> print,Shift(dist(5),2,2)

2.82843 2.23607 2.00000 2.23607 2.82843 
2.23607 1.41421 1.00000 1.41421 2.23607 
2.00000 1.00000 0.000000 1.00000 2.00000 
2.23607 1.41421 1.00000 1.41421 2.23607 
2.82843 2.23607 2.00000 2.23607 2.82843
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 1
    You could def. roll your own with e.g. [np.linalg.norm](https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy). I'm not aware of a numpy builtin for this, but it's not my field. – 2e0byo Mar 20 '23 at 16:17
  • `[[math.hypot(min(i, n-i), min(j, n-j)) for j in range(n)] for i in range(n)]` – Rodrigo Rodrigues Mar 20 '23 at 16:34
  • You can check [this answer](https://stackoverflow.com/a/75793502/2938526) for combined `dist` and `shift`. – Rodrigo Rodrigues Mar 20 '23 at 17:33

0 Answers0