MATLAB: How to change every value not equal to X in every rows

change rowsloopmatrix manipulation

Hi,
I have a matrix (20000×365) and I have a vector(20000×1) in which I have the column assiciate with the number that I DON'T want to change (This number change for every rows) .
For every rows I want to change every colums, beside the one that is in the vector, for 0
EX: matrix [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
Vector [3,1,3,2]
The answers should be [0 0 1 0; 8 0 0 0; 0 0 6 0; 0 3 0 0]
Thanks for your help!

Best Answer

Wanted = zeros(size(matrix));
indices=sub2ind(size(matrix),1:size(matrix,1),Vector);
Wanted(indices)=matrix(indices)