![](https://secure.gravatar.com/avatar/6fdbe558d5fc70735afc27d41ef9abb7.jpg?s=120&d=mm&r=g)
Is there a numpy function to convert corresponding array indices in flattened / nonflat multidimensional arrays for a given shape? E.g. for a = array([0,1,2,3,4,5]).reshape(2,3) I want some function that converts e.g. 1 to [0,1] and 5 to [1,2] if I tell it a.shape. For 2D it's of course easy to do it by hand, but I need something that is fast and works for arrays of any dimension.
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Mon, Mar 8, 2010 at 2:02 PM, Christoph Deil <Deil.Christoph@googlemail.com> wrote:
Is there a numpy function to convert corresponding array indices in flattened / nonflat multidimensional arrays for a given shape?
E.g. for a = array([0,1,2,3,4,5]).reshape(2,3) I want some function that converts e.g. 1 to [0,1] and 5 to [1,2] if I tell it a.shape. For 2D it's of course easy to do it by hand, but I need something that is fast and works for arrays of any dimension.
in numpy docs under Indexing Routines
list(np.ndindex(3,2,4)) [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3)]
there are also other iterators and functions Josef
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/6a1dc50b8d79fe3b9a5e9f5d8a118901.jpg?s=120&d=mm&r=g)
On Mon, Mar 8, 2010 at 11:02 AM, Christoph Deil <Deil.Christoph@googlemail.com> wrote:
Is there a numpy function to convert corresponding array indices in flattened / nonflat multidimensional arrays for a given shape?
E.g. for a = array([0,1,2,3,4,5]).reshape(2,3) I want some function that converts e.g. 1 to [0,1] and 5 to [1,2] if I tell it a.shape. For 2D it's of course easy to do it by hand, but I need something that is fast and works for arrays of any dimension.
You could create a dictionary using numpy.ndenumerate. Or you could use a third party package such as the labeled array package, la:
a = np.array([0,1,2,3,4,5]).reshape(2,3) from la import larry y = larry(a) y.todict() {(0, 0): 0, (0, 1): 1, (0, 2): 2, (1, 0): 3, (1, 1): 4, (1, 2): 5}
![](https://secure.gravatar.com/avatar/6401b8425eed08fcbaffffeeaceac894.jpg?s=120&d=mm&r=g)
On Mon, Mar 8, 2010 at 1:02 PM, Christoph Deil <Deil.Christoph@googlemail.com> wrote:
Is there a numpy function to convert corresponding array indices in flattened / nonflat multidimensional arrays for a given shape?
E.g. for a = array([0,1,2,3,4,5]).reshape(2,3) I want some function that converts e.g. 1 to [0,1] and 5 to [1,2] if I tell it a.shape. For 2D it's of course easy to do it by hand, but I need something that is fast and works for arrays of any dimension.
Look at numpy.unravel_index: Convert a flat index to an index tuple for an array of given shape. Parameters ---------- x : int Flattened index. dims : tuple of ints Input shape, the shape of an array into which indexing is required. Returns ------- idx : tuple of ints Tuple of the same shape as `dims`, containing the unraveled index. Notes ----- In the Examples section, since ``arr.flat[x] == arr.max()`` it may be easier to use flattened indexing than to re-map the index to a tuple. Examples -------- >>> arr = np.arange(20).reshape(5, 4) >>> arr array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]) >>> x = arr.argmax() >>> x 19 >>> dims = arr.shape >>> idx = np.unravel_index(x, dims) >>> idx (4, 3) >>> arr[idx] == arr.max() True Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma
participants (4)
-
Christoph Deil
-
josef.pktd@gmail.com
-
Keith Goodman
-
Ryan May