How to implement matlab's ismember(A, b) with A being a numpy ndarray and b being a list
Question: What is the most efficient way to implement matlab's [ismember(A,
b)](http://www.mathworks.com/help/matlab/ref/ismember.html) in python where in
A is any numpy ndarray and b is a list of values. It should return a mask as a
boolean ndarray of the same shape as A where in an element is True if the
corresponding value in A is in the list of values in b.

I want to replace all elements of A with value in list B with something.

I expected `A[A in B] = 0` to work but it throws the following error:

> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()

If an implementation of an equivalent of `ismember` is there then the
following would do what I need:

    
    
    A[ismember(A, b)] = 0
    

Note: I don't want solutions involving looping through all elements of A in
python.

Based on the answer of [ajcr](http://stackoverflow.com/users/3923281/ajcr),
one solution is:

    
    
    import numpy as np
    
    def ismember(A, b):
      return np.in1d(A, b).reshape(A.shape)
    

But this is quite slow and runs out of memory. For my case, A is an image as
big as 512 x 512 x 1200. b has about 1000 elements.


Answer: You may be looking for
[`np.in1d`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.in1d.html):

    
    
    >>> A = np.arange(9)
    >>> B = [4, 6, 7]
    >>> np.in1d(A, B)
    array([False, False, False, False,  True, False,  True,  True, False])
    

Note that for multidimensional arrays `A`, the input is flattened so you'll
need to reshape the boolean array:

    
    
    >>> A = np.arange(9).reshape(3, 3)
    >>> np.in1d(A, B).reshape(A.shape)
    array([[False, False, False],
           [False,  True, False],
           [ True,  True, False]], dtype=bool)
    

