1

I have a 3D array created using the numpy mgrid command so that each element has a certain value and the indexes retain the spatial information. For example, if one summed over the z-axis (3rd dimension) then the the resultant 2D array could be used in matplotlib with the function imshow() to obtain an image with different binned pixel values.

My question is: How can I obtain the index values for each element in this grid (a,b,c)?

I need to use the index values to calculate the relative angle of each point to the origin of the grid. (eg. theta=sin-1(sqrt(x^2+y^2)/sqrt(x^2+y^2+z^2))

Maybe this can be translated to another 3D grid where each element is the array [a,b,c]?

1
  • 1
    np.indicies() does that. Commented Jul 2, 2011 at 1:10

1 Answer 1

1

I'm not exactly clear on your meaning, but if you are looking for 3d arrays that contain the indices x, y, and z, then the following may suit your needs; assume your data is held in a 3D array called "abc":

import numpy as nm
x,y,z = nm.mgrid[[slice(dm) for dm in abc.shape]]
Sign up to request clarification or add additional context in comments.

1 Comment

For @Sebastian's purposes it's much more efficient to generate an open grid using np.ogrid - numpy's broadcasting rules will mean that the result of his computation will be 3D without having to allocate 3 whole xyz matrices of indices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.