[SciPy-User] equivalent of ind2sub

Angus McMorland amcmorl at gmail.com
Fri Apr 9 08:21:36 EDT 2010


On 8 April 2010 18:23, Anil C R <cr.anil at gmail.com> wrote:

> is there any function which is similar to matlab function ind2sub?


numpy.unravel_index

docstring:

 Convert a flat index into an index tuple for an array of given shape.

    Parameters
    ----------
    x : int
        Flattened index.
    dims : shape tuple
        Input shape.

    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


-- 
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100409/63d46ddc/attachment.html>


More information about the SciPy-User mailing list