[Numpy-discussion] a[j, k], clipping j k to the edge if they're 1 off ?

denis bzowy denis-bz-py@t-online...
Fri Aug 28 11:14:36 CDT 2009


  I want to index a[j,k], clipping j or k to the edge if they're 1 off --

def aget( a, j, k ):
    """ -> a[j,k]  or a[edge] """
        # try:
        #    return a[j,k]  -- nope, -1
        # except IndexError:
    m,n = a.shape
    return a[ min(max(j, 0), m-1), min(max(k, 0), n-1)]

This works but is both ugly and 5* slower than plain a[j][k].
Is there a better way ?

(Sorry if this is a duplicate, must come up often.)
  -- denis

More information about the NumPy-Discussion mailing list