[Numpy-discussion] variable end border in arrays

Sebastian Berg sebastian at sipsolutions.net
Fri Jun 5 03:55:16 EDT 2015

On Fr, 2015-06-05 at 15:17 +0800, Pablo wrote:
> Hi,
> If I want to remove 1 element in the beginning and the end of a numpy 
> array "x" we do:
> x[1:-1]
> Now, if we have a border variable, and borders are allowed to be zero 
> (which means no border), numpy syntax is inconvenient. For example if 
> border=numpy.asarray([1,0]) and we try
> x[border[0],-border[1]]
> it will produce an empty array because border[1]==0 is not considered 
> respect to the end of the array anymore.
> Is it possible to solve this without if/else's ? (I work with images and 
> tensor's so if/else's have too many possible combinations)

Yes and no. You could do if/else and use a None. Or just use the
positive index: x[border[0]:x.shape[0] - border[1]].

- Sebastian

> Thanks,
> Pablo
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150605/8fe8822c/attachment.sig>

More information about the NumPy-Discussion mailing list