MATLAB: Replace elements of matrix

replace value in matrix

I have the folowing:
vector=[1 3 8 9];
matrix=[ 100 1 5 9 6; 100 10 13 3 8; 100 9 10 1 4; ];
% I want to search and replace the vector element with "0"in the matrix (i.e new matrix should be : Newmatrix=[ 100 0 5 0 6; 100 10 13 0 0; 100 0 10 0 4; ]; )
The script is:
Newmatrix=zeros(size(matrix));
for i=1:numel(matrix)
for j=1:length(vector)
valvect=vector(j);
if matrix(i)==valvect
Newmatrix(i)=0;
else
Newmatrix(i)=matrix(i);
end
end
end
The results is not the desired one but:
Newmatrix=100 1 5 0 6
100 10 13 3 8
100 0 10 1 4
So what I'm doing wrong?
Thank you

Best Answer

hi, try ;
F=matrix;
for i=1:length(vector)
F(F==vector(i))=0;
end