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

Robert Kern robert.kern at gmail.com
Fri Aug 28 12:28:18 EDT 2009


On Fri, Aug 28, 2009 at 09:14, denis bzowy<denis-bz-py at t-online.de> wrote:
> Folks,
>
>  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 ?

Nope.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list