This recipe finds the complement of a set of indices from a specific arange(n) array. Suppose, for example, you are given a linear array with 10 elements and you want to extract the elements from this array that have indices other than [1, 3, 5]. You can then use this recipe to first find the complement of [1, 3, 5], and you can then use numarray.take() to extract the elements of interest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/usr/bin/env python
import numarray
def complement(ind_arr, n):
"""
Find the complement of the set of indices in ind_arr from
arange(n)
"""
mat = numarray.ones(n)
numarray.put(mat, ind_arr, 0)
out = numarray.nonzero(mat)
return out[0]
if __name__ == "__main__":
orig_arr = numarray.arange(10) + 0.2
indices = numarray.array([1, 3, 5])
comp = complement(indices, len(orig_arr))
comp_arr = numarray.take(orig_arr, comp)
print "orig_arr: ", orig_arr
print "indices: ", indices
print "complement indices: ", comp
print "complement elements: ", comp_arr
|
Tags: graphics