function D = meanWoutNaN(D, dim)
% D - N-dimensional data matrix.
% dim - Dimension to calculate mean over.
% D - (N-1)-dimensional data matrix. In Matlab dimension dim is
% retained and set to the value of 1.
% Calculate the mean of D along dimension dim excluding NaN entries.
% Florian Raudies, 09/07/2014, Boston University.
Index = isnan(D);
D = sum(D,dim)./(eps+size(D,dim)-sum(Index,dim));