array slicing question
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.
On 03.04.2005, at 15:31, Gary Ruben wrote:
You might think you could do
p=1 b = a[p:-p]
but if p=0, this fails.
b = a[p:len(a)-p] works even for p=0. Konrad. -- ------------------------------------------------------------------------ ------- Konrad Hinsen Laboratoire Leon Brillouin, CEA Saclay, 91191 Gif-sur-Yvette Cedex, France Tel.: +33-1 69 08 79 25 Fax: +33-1 69 08 82 61 E-Mail: khinsen@cea.fr ------------------------------------------------------------------------ -------
Thanks Konrad, Sorry, my example was too simple. The actual example representing an image should have been 2-D and not necessarily square. Therefore I used shape instead of len and it seemed messy doing it this way. Gary konrad.hinsen@laposte.net wrote:
On 03.04.2005, at 15:31, Gary Ruben wrote:
You might think you could do
p=1 b = a[p:-p]
but if p=0, this fails.
b = a[p:len(a)-p] works even for p=0.
Konrad. -- ------------------------------------------------------------------------ ------- Konrad Hinsen Laboratoire Leon Brillouin, CEA Saclay, 91191 Gif-sur-Yvette Cedex, France Tel.: +33-1 69 08 79 25 Fax: +33-1 69 08 82 61 E-Mail: khinsen@cea.fr ------------------------------------------------------------------------ -------
Gary Ruben <gruben@bigpond.net.au> writes:
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]
b = a[p:-p or None] 'as
participants (3)
-
Alexander Schmolck -
Gary Ruben -
konrad.hinsen@laposte.net