loop through every element of matrix for thresholding