[Numpy-discussion] I,J,K Coordinates from Cell ID
Thu Feb 28 17:16:13 CST 2008
On Thu, Feb 28, 2008 at 1:47 PM, Andrea Gavana <email@example.com>
> Hi All,
> I have some problems in figuring out a solution for an issue I am
> trying to solve. I have a 3D grid of dimension Nx, Ny, Nz; for every
> cell of this grid, I calculate the cell centroids (with the cell
> coordinates x, y, and z) and then I try to find which cell centroid is
> the closest to a specified point in 3D (which I supply). I still
> haven't figured out how to do this, even if I have some ideas (and
> suggestions for this problem are welcome :-D ). But the problem is
> another one.
> When I find the closest centroid, I know only the cell ID of this
> centroid. The cell ID is (usually) defined as:
> ID = (K-1)*Nx*Ny + (J-1)*Nx + I - 1
> Where I, J, K are the cell indexes. Now, the problem is, how can I
> calculate back the I, J, K indexes knowing only the cell ID? I am
> trying to solve this using numpy (as my grid is stored using a numpy
> array), but it's something akin to the Matlab function ind2sub...
> Thank you for your suggestions.
I think you are going to want to use mod (aka '%'). Something like:
>>> def coord(id):
... return (id % NX + 1, id // NX % NY + 1, id // (NX*NY) + 1 )
should work. I believe there are a few things you could do to improve the
efficiency here, but try this and see if it works for you before you worry
Note that the above definition for cell ID is probably a little weird in the
context of numpy where all of the indexing starts at zero.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion