[Numpy-discussion] array slicing question

Gary Ruben gruben at bigpond.net.au
Sun Apr 3 06:32:09 CDT 2005


This may be relevant to Numeric 3, but is possibly just a general 
question about array slicing which will either reveal a deficiency in 
specifying slices or in my knowledge of slicing with numpy.
A while ago I was trying to reimplement some Matlab image processing 
code in Numeric and revealed a deficiency in the way slices are defined. 
Suppose I have an n x m array and want to slice off the first and last p 
rows and columns where p can range from 0 to some number. Matlab 
provides a clean way of doing this, but in numpy it's a bit of a mess.

You might think you could do
 >>> p=1
 >>> b = a[p:-p]

but if p=0, this fails.
My final solution involved getting the array shape and explicitly 
calculating start and stop columns, but is there a better way?

Gary R.




More information about the Numpy-discussion mailing list