threshold matrix to a certain cost