Thank you vary much Ben and Erik. It's exactly what I want. Below is my a little modified example. set.seed(12345) X = sample(c(40:60),40,replace=TRUE) x = matrix(X,nc=5) y = c(40,43,55,60) x[x %in% y] <- -1