1 # Copyright (C) 2008 Sylvain Pelissier <sylvain.pelissier@gmail.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {} nanvar (@var{x})
18 ## @deftypefnx{Function File} {@var{v} =} nanvar (@var{X}, @var{opt})
19 ## @deftypefnx{Function File} {@var{v} =} nanvar (@var{X}, @var{opt}, @var{dim})
20 ## Compute the variance while ignoring NaN values.
22 ## For vector arguments, return the (real) variance of the values.
23 ## For matrix arguments, return a row vector containing the variance for
26 ## The argument @var{opt} determines the type of normalization to use.
31 ## Normalizes with @math{N-1}, provides the best unbiased estimator of the
32 ## variance [default].
34 ## Normalizes with @math{N}, this provides the second moment around the mean.
37 ## The third argument @var{dim} determines the dimension along which the
38 ## variance is calculated.
40 ## @seealso{var, nanmean, nanstd, nanmax, nanmin}
43 function y = nanvar(x,w,dim)
47 if ((nargin < 2) || isempty(w))
52 dim = min(find(size(x)>1));
58 y = nanstd(x,w,dim).^2;
64 %! x = [1 2 nan 3 4 5];
65 %!assert (nanvar (x), var (x(! isnan (x))), 10*eps)