It'w very bad practice to call your image i (the imaginary variable). Call it binaryImage, mask, or BW instead. You might want to call imclearborder() to get rid of that outer blob, unless you want it.
mask = imclearborder(mask);
I don't believe you need to use
because ismember() will return a logical image already.
Other than that, David's solution should work.