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?
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 writebefore 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 particularlynanmeanetc are functions of the statistics toolbox whereNaNs aren’t simply replaced by zero, but actually removed from the calculation, because in the context of statistics,NaNis used to indicate missing data points. There is nonanmultout 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.