How to put a condition accordingly the matrix values?