I have a simulation of particles moving on a grid, created with numpy.meshgrid, and I want to determine the distance of every point on the grid to the closest particle. I’ve already found the closest particles, and stored them in a 2d array closest
which has the same dimensions as the grid and contains the index of the particle closest to the corresponding point. I also have the 3d array distances
with dimensions (number of parties, x size, y size) and which holds every point’s distance to every particle.
I need to do calculations with the distance of each point to the closest particle.
I made the mistake of using numpy advanced indexing without fully understanding it. I tried
distances[closest[0], closest[1]]
with the intention of returning a matrix of distances to the closest particles. This was fast and produced the right shape of output, but is incorrect. What did I do wrong?
For now I have fixed the issue with explicit for loops, but since this is in my code’s innermost loop I would like to make better use of numpy’s speed. This code works:
for x in range(0, np.size(self.weight, 0)):
for y in range(0, np.size(self.weight, 1)):
closestDistances[x, y] = distances[closest[x, y], x, y]
Here self.weight
is just there to get the dimensions of the grid. Thanks in advance!
user25918222 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.