[SciPy-User] flat / nonflat array index conversion

Keith Goodman kwgoodman at gmail.com
Mon Mar 8 14:11:42 EST 2010


On Mon, Mar 8, 2010 at 11:02 AM, Christoph Deil
<Deil.Christoph at 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}



More information about the SciPy-User mailing list