Mathematica subtracts matrices with missing value

In Mathematica, I have a matrix 'a' with missing values, and I have a matrix 'b' with the same size as 'a'. I would like to calculate ab, but if the value is missing, which I stand for "NA", I would like it to remain as "NA". could you help me? Please note that β€œa” has a size of 1 million X300.

Thank!

+5
source share
2 answers

One approach is to use a replacement rule for the result, something like this:

In[1]  {1, na, 3, na, 5} - {1, 2, 3, 4, 5}
Out[1] {0, -2 + na, 0, -4 + na, 0}

In[2]  {1, na, 3, na, 5} - {1, 2, 3, 4, 5}/. x_ + na -> na
Out[2] {0, na, 0, na, 0}

Another approach would be to define UpValuefor naso that addition (and subtraction) with it has always led to na; eg:

In[3] na /: Plus[___, na, ___] := na

UpValues , , .

+7

Indeterminate , , :

In[2]:= {1, na, 3, na, 5} - {1, 2, 3, 4, 5} /. na -> Indeterminate /. Indeterminate -> na

Out[2]= {0, na, 0, na, 0}

Indeterminate NA.

+4

All Articles