MATLAB: How to replace elements different than 0 and different than NaN to 1

nan valuesreplacezeros and ones

I have this huge matrix with a lot of elements: (i) equal to zero; (ii) number different than zero; (iii) NaN values …
I need to replace all the the NUMBERS different than 0 to 1.
How do I do it not replacing the NaN values?

Best Answer

Let the matrix name, A, so this is done by:
A(xor((A~=0),isnan(A))) = 1 ;