Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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]?

share|improve this question
1  
np.indicies() does that. –  tillsten Jul 2 '11 at 1:10

1 Answer 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]]
share|improve this answer
    
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 –  ali_m Dec 22 '13 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.