Sanjay Manohar
Sanjay Manohar

Reputation: 7026

Matlab Matrix multiplication ignoring nans?

I have two matrices A and B which I want to multiply, but they contain nans.

The default multiply puts nan down the whole column of the result where there was even a single nan in the data. I want to ignore them, like nansum/nanmean etc do. In other words, instead of computing

 sum( A(i,j) * B(j,k) )

I want it to use nansum.

I suppose this is possible by replacing nan with 0, and then multiplying, but the point of having nansum is to obviate that, right?

Upvotes: 2

Views: 10160

Answers (2)

Erin Fowler
Erin Fowler

Reputation: 51

import numpy as np

#find the dimensions of your arrays
sza=a.shape
szb=b.shape

#calculate by using nested loops rows of 'a' times columns of 'b'
c=np.zeros(sza[0],szb[1])
for i in range(0, sza[0]):
  for k in range(0, szb[1]):
    c[i, k]=np.nansum(a[i, ]*b[:, k])

Upvotes: 2

Jonas
Jonas

Reputation: 74940

The reason the multiplication results in NaNs is that there is no one true way in which they should be handled. If, in your case, they should be replaced by zero, it's easiest to write

A(isnan(A)) = 0;

before running the multiplication. I would advise against re-writing matrix multiplication, since you won't get similar performance to built-in matrix algebra out of your own code.

nansum, and particularly nanmean etc are functions of the statistics toolbox where NaNs aren't simply replaced by zero, but actually removed from the calculation, because in the context of statistics, NaN is used to indicate missing data points. There is no nanmult out there, because in statistics, you don't often matrix-multiply, and if you do (e.g. within a regression), having a single observation missing from a vector usually means that you'll want to throw out the entire row/column, anyway.

Upvotes: 5

Related Questions