[Numpy-discussion] Slicing a numpy array and getting the "complement"

Robert Kern robert.kern at gmail.com
Mon May 19 12:47:52 EDT 2008


On Mon, May 19, 2008 at 9:34 AM, Orest Kozyar <orest.kozyar at gmail.com> wrote:
> Given a slice, such as s_[..., :-2:], is it possible to take the
> complement of this slice?  Specifically, s_[..., ::-2].

Hmm, that doesn't look like the complement. Did you mean s_[..., -2:]
and s_[..., :-2]?

> I have a
> series of 2D arrays that I need to split into two subarrays via
> slicing where the members of the second array are all the members
> leftover from the slice.  The problem is that the slice itself will
> vary, and could be anything such as s_[..., 1:4:] or s_[..., 1:-4:],
> etc, so I'm wondering if there's a straightforward idiom or routine in
> Numpy that would facilitate taking the complement of a slice?  I've
> looked around the docs, and have not had much luck.

In general, for any given slice, there may not be a slice giving the
complement. For example, the complement of arange(6)[1:4] should be
array([0,4,5]), but there is no slice which can make that. Things get
even more difficult with start:stop:step slices let alone simultaneous
multidimensional slices. Can you be more specific as to exactly the
variety of slices you need to support?

-- 
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