MATLAB: Subtracting different matrices with NaN values

ignoring nannan

Hello everybody,
i have the following problem. There are 4 matrices (each containing 234×7690), i will call them A B C D. Then I have to calculate the result of A – B – C – D. All of the matrices have multiple Nan's. So if one of the 4 matrices has no value the result is NaN for this cell. But i need a result whenever A is a real number and at least one of the other has also a real value. So for example if (36 – Nan – 5 – NaN) the result should be 31. I hope you got my problem. Thanks for your answers.

Best Answer

Just replace NaNs with 0s
e.g.
B( isnan(B) ) = 0;