[SciPy-User] equivalent of ind2sub

Anil C R cr.anil@gmail....
Fri Apr 9 06:37:43 CDT 2010


hey guys, I wrote my own function which does what ind2sub does:

############################CODE###################################
def ind2sub( dim, shap, lst ):
    '''
    I1, I2, I3, ..., Idim = ind2sub(dim, shap, lst)
    Input:
        dim - number of dimensions
        shap - shape of the array
        lst - list of indicies
    Output:
        list of subscripts
    r = array([[0,1,0,0],[0,0,1,1],[1,1,1,0],[1,0,0,1]])
    l = find(r==1)
    [cols,rows] = ind2sub(2, r.shape, l)
    # The element in coulmn cols[i], and row rows[i] is 1.
    '''
    if len(shap) <= dim:
        shap = shap + tuple(zeros((dim - len(shap),)))
    else:
        shap = shap[0:dim-1] + (prod(shap[(dim-1):]),)

    n = len(shap)
    k = array([1] + cumprod(shap[0:(n-1)]).tolist())

    argout = [zeros((len(lst),))]*n

    for i in xrange(n-1,-1,-1):
        vi = (lst)%k[i]
        vj = (lst-vi)/k[i]
        argout[i] = vj
        lst = vi
    return argout
##########################END CODE#################################

Anil


On Fri, Apr 9, 2010 at 1:03 AM, eat <e.antero.tammi@gmail.com> wrote:

> Anil C R <cr.anil <at> gmail.com> writes:
> >
> >
> > is there any function which is similar to matlab function ind2sub?
> >
> > Anil
> >
> Hi,
>
> Would you like to give some use case(s) of ind2sub?
>
>
> Regards,
> eat
> >
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User <at> scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
>
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100409/f7e5751e/attachment.html 


More information about the SciPy-User mailing list