[SciPy-User] flat / nonflat array index conversion

josef.pktd@gmai... josef.pktd@gmai...
Mon Mar 8 13:11:36 CST 2010


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
>


More information about the SciPy-User mailing list