0001 function y=nt_wmean(x,w,dim)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 if nargin<3; dim=[]; end
0013 if nargin<2; w=[]; end
0014
0015
0016 if numel(find(isnan(x)))>0
0017 x(find(isnan(x)))=0;
0018 if isempty(w)
0019 w=ones(size(x));
0020 end
0021 w(find(isnan(x)))=0;
0022 end
0023
0024 if isempty(dim); dim=1; end
0025 if isempty(w)
0026 y=mean(x,dim);
0027 else
0028 if size(x,1) ~= size(w,1); error('data and weight must have same nrows'); end
0029 if size(w,2)==1; w=repmat(w,1,size(x,2)); end
0030 if size(w,2) ~= size(x,2); error('weight must have same ncols as data, or 1'); end
0031 y=sum(x.*w,dim)./sum(w,dim);
0032 y(find(isnan(y)))=0;
0033 end
0034