MATLAB: Turn pixels in connected components to zero based on logical array

arrayconnected components

I want to switch all pixels in connected components to zero, based on a logical array.
I have CC.PixelIdxList obtained from bwconncomp performed on binary image BW.
I have a logical array, X, the size of CC.PixelIdxList, based on which I would like to turn pixels in CC.PixelIdxList{y} to zero if the value in X=0 at index y.
I tried this:
BW(CC.PixelIdxList{y}) =X;
where y is just an array of numbers from 1 to the size of CC.PixelIdxList.
But it is not working, there must be another way that I cant think of.
Appreciate any suggestions. Thanks.

Best Answer

BW(CC.PixelIdxList{y}(~X)) = 0;