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

Orest Kozyar orest.kozyar at gmail.com
Mon May 19 14:21:33 EDT 2008


> If you don't mind fancy indexing, you can convert your index arrays
> into boolean form:
> complement = A==A
> complement[idx] = False

This actually would work perfectly for my purposes.  I don't really
need super-fancy indexing.

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

Whoops, yes you're right.

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

I think Anne's solution will work well for what I need to do.  Thanks!



More information about the NumPy-Discussion mailing list