MATLAB: Using a column vector of indices to replace values in a matrix

linear indexinglogical indexingMATLABmatrixmatrix manipulation

Hello,
I have a column vector, C, (55×1 double) which are the indices of my values of interest in Matrix, M which is 370×29 double.
I want to replace all the values in M that are not these values of interest with 0.
I am trying to use C to index into M and replace ~C with 0.
However when I try
M(~C)=0
all it does is change all the values in M to just different values and I'm not sure why.
When I try to create a new Matrix
N=M(~C==0) it returns a columnn vector with just the first 55 values in the first column of M.
Does anyone know how I can replace all the elements in M that are not listed in C with 0? I want my output to be another 370×29 double matrix.
Thanks!

Best Answer

C=[1,2,3]';
M=magic(5);
index=setdiff(1:numel(M),C);
M(index)=0