diff --git a/matlab/utilities/general/demean.m b/matlab/utilities/general/demean.m
index 741a2f10778a15e7f87de593b6cfd1e6eb6647bc..b9dda8d13161b3b2c3c153542aeccedd75feff60 100644
--- a/matlab/utilities/general/demean.m
+++ b/matlab/utilities/general/demean.m
@@ -47,9 +47,9 @@ function c = demean(x)
 % Original author: stephane DOT adjemian AT univ DASH lemans DOT fr
 
 if ndim(x)==1
-    c = x-mean(x);
+    c = x-nanmean(x);
 elseif ndim(x)==2
-    c = bsxfun(@minus,x,mean(x));
+    c = bsxfun(@minus,x,nanmean(x));
 else
     error('descriptive_statistics::demean:: This function is not implemented for arrays with dimension greater than two!')
 end
\ No newline at end of file