Let's suppose I have a matrix A=
1 2 3;
4 5 6;
7 8 9
and a matrix B=
1 NaN 3;
NaN 5 6;
7 NaN NaN
I want to cancel the same elements that are canceled in matrix B. It would be:
A2=
1 NaN 3;
NaN 5 6;
7 NaN NaN
How can I do that?
You could create the new matrix A2 such that each element is
a2 = b - b + a
.This relies on the fact that NaN propagates through the expression evaluation, and
b - b
is essentially a no-op in all other instances.It is also guaranteed never to overflow your type.