def setmember1d(ar1, ar2): """ Return a boolean array set True where first element is in second array. See Also -------- numpy.setmember1d : equivalent function for ndarrays. """ ar1 = ma.asanyarray(ar1) ar2 = ma.asanyarray(ar2) ar = ma.concatenate((ar1, ar2)) b1 = ma.zeros(ar1.shape, dtype=np.int8) b2 = ma.ones(ar2.shape, dtype=np.int8) tt = ma.concatenate((b1, b2)) # We need this to be a stable sort, so always use 'mergesort' here. The # values from the first array should always come before the values from the # second array. perm = ar.argsort(kind="mergesort") aux = ar[perm] aux2 = tt[perm] # flag = ediff1d( aux, 1 ) == 0 flag = ma.concatenate((aux[1:] == aux[:-1], [False])) ii = ma.where(flag * aux2)[0] aux = perm[ii + 1] perm[ii + 1] = perm[ii] perm[ii] = aux # indx = perm.argsort(kind="mergesort")[: len(ar1)] # return flag[indx]
def setmember1d(ar1, ar2): """ Return a boolean array set True where first element is in second array. See Also -------- numpy.setmember1d : equivalent function for ndarrays. """ ar1 = ma.asanyarray(ar1) ar2 = ma.asanyarray( ar2 ) ar = ma.concatenate((ar1, ar2 )) b1 = ma.zeros(ar1.shape, dtype = np.int8) b2 = ma.ones(ar2.shape, dtype = np.int8) tt = ma.concatenate((b1, b2)) # We need this to be a stable sort, so always use 'mergesort' here. The # values from the first array should always come before the values from the # second array. perm = ar.argsort(kind='mergesort') aux = ar[perm] aux2 = tt[perm] # flag = ediff1d( aux, 1 ) == 0 flag = ma.concatenate((aux[1:] == aux[:-1], [False])) ii = ma.where( flag * aux2 )[0] aux = perm[ii+1] perm[ii+1] = perm[ii] perm[ii] = aux # indx = perm.argsort(kind='mergesort')[:len( ar1 )] # return flag[indx]
def setmember1d(ar1, ar2): """ This function is deprecated. Use ma.in1d() instead.""" ar1 = ma.asanyarray(ar1) ar2 = ma.asanyarray( ar2 ) ar = ma.concatenate((ar1, ar2 )) b1 = ma.zeros(ar1.shape, dtype = np.int8) b2 = ma.ones(ar2.shape, dtype = np.int8) tt = ma.concatenate((b1, b2)) # We need this to be a stable sort, so always use 'mergesort' here. The # values from the first array should always come before the values from the # second array. perm = ar.argsort(kind='mergesort') aux = ar[perm] aux2 = tt[perm] # flag = ediff1d( aux, 1 ) == 0 flag = ma.concatenate((aux[1:] == aux[:-1], [False])) ii = ma.where( flag * aux2 )[0] aux = perm[ii+1] perm[ii+1] = perm[ii] perm[ii] = aux # indx = perm.argsort(kind='mergesort')[:len( ar1 )] # return flag[indx]
def setmember1d(ar1, ar2): """ This function is deprecated. Use ma.in1d() instead.""" ar1 = ma.asanyarray(ar1) ar2 = ma.asanyarray(ar2) ar = ma.concatenate((ar1, ar2)) b1 = ma.zeros(ar1.shape, dtype=np.int8) b2 = ma.ones(ar2.shape, dtype=np.int8) tt = ma.concatenate((b1, b2)) # We need this to be a stable sort, so always use 'mergesort' here. The # values from the first array should always come before the values from the # second array. perm = ar.argsort(kind='mergesort') aux = ar[perm] aux2 = tt[perm] # flag = ediff1d( aux, 1 ) == 0 flag = ma.concatenate((aux[1:] == aux[:-1], [False])) ii = ma.where(flag * aux2)[0] aux = perm[ii + 1] perm[ii + 1] = perm[ii] perm[ii] = aux # indx = perm.argsort(kind='mergesort')[:len(ar1)] # return flag[indx]
def ediff1d(arr, to_end=None, to_begin=None): """ Computes the differences between consecutive elements of an array. This function is the equivalent of `numpy.ediff1d` that takes masked values into account. See Also -------- numpy.eddif1d : equivalent function for ndarrays. Returns ------- output : MaskedArray """ arr = ma.asanyarray(arr).flat ed = arr[1:] - arr[:-1] arrays = [ed] # if to_begin is not None: arrays.insert(0, to_begin) if to_end is not None: arrays.append(to_end) # if len(arrays) != 1: # We'll save ourselves a copy of a potentially large array in the common # case where neither to_begin or to_end was given. ed = hstack(arrays) # return ed