Tuesday, 15 March 2011

python - Pythonic way to create a numpy array of coordinates -


I am trying to create a numpy array of coordinates. I have so far, I just x_coords, y_coords = numpy .indices (using the size)) . Now, however, I want to add x_coords and y_coords to an array such as x_coords = thisArray [:,:, 0] and y_coords = thisArray [,:,, 1] In this case, this array is a two-dimensional array, is there a simple or python way to do this?

I originally thought of using the numpy.outer , but that does not give me anything which I want. A possible idea is just using a combination of index array with (2?) Axis, but it does not look like a very elegant solution. (This can be the most obvious here).

Thank you!

What is the np.indices return already has an array, but < Code> x_coords = thisArray [0,:,:] and y_coords = thisArray [1,:,:] . Unless you have very strict requirements for your array coordinates (i.e. that is adjacent), you can take that view's view with the axis first by the end:

  thisArray = numpy.rollaxis (Numpy.indices (shape), 0, lane (size) + 1)    

No comments:

Post a Comment