MATLAB: How to center a matrix containing NaN values

centermatrixnan

I have a matrix of size 72×144. It contains many NaN values, but also contains numeric values. I have been successful in the past at centering matrices containing only numeric values using the following code:
xctr=bsxfun(@minus, x, sum(x)/size(x, 1));
However, this seems to output a matrix with all NaN values, rather than giving me many NaN values, and also the centered numerical values. How can I center the data without turning everything into NaN?

Best Answer

if you statistic toolbox
xctr=bsxfun(@minus, x, nansum(x)/size(x, 1));