First we construct a matrix that has some NaN in:
A = [NaN NaN 1 NaN NaN;
NaN 1 2 1 NaN;
1 2 3 2 1;
NaN 1 2 1 NaN;
NaN NaN 1 NaN NaN];
1. If you want to convert NaN to 0:
isnan(A) will identify the elements that are NaN's; " = 0" will turn them into 0 (or any other value!).
2. If you can't do this (as mentioned in the question), you could use an imaginary number to recognise that there used to be a NaN in that position.
All your previous numbers are still there (the real value), whilst your NaN's are now imaginary values of 1 (1j). You can access each with
Meaning you can now add the matrix real(A) to another matrix, which would ignore the NaN values of A, and then check to see where the NaN's of A were via imag(A).
3. If you can't use imaginary numbers (perhaps you are already using the complex plane in your work), perhaps you could use the previous method of changing NaN's to zeros, but then create another matrix of the same dimentions/size and log where the NaN's were in that?
A = [ NaN+j*NaN NaN 1 NaN NaN;
NaN 1+1j 2 1 NaN;
1 2+1j 3+1j 2 1;
NaN 1+1j 2 1+1j NaN;
NaN+j*NaN NaN 1+1j NaN NaN];
A_nan = zeros(size(A));
A_nan(isnan(real(A))) = 1;
A_nan(isnan(imag(A))) = 1j+A_nan(isnan(imag(A)));
A(isnan(imag(A))) = NaN + 0j;
A(isnan(real(A))) = 0;
It should be noted that for there to be an imaginary NaN, the real value must be NaN as well.
You can't have 1 + NaN*i; it must be NaN + NaN*i.
There is a work around in MATLAB, but it's probably going to be more trouble than it's worth.
There is another page on this here.