满意答案
nansum 是统计工具箱(Statistics Toolbox)里面的函数,请确认是否安装了该工具箱。
如果不想装工具箱,我把函数的代码给你,可以独立使用(不过最好还是安装工具箱):
nansum.mfunction y = nansum(x,dim)
%NANSUM Sum, ignoring NaNs.
% Y = NANSUM(X) returns the sum of X, treating NaNs as missing values.
% For vector input, Y is the sum of the non-NaN elements in X. For
% matrix input, Y is a row vector containing the sum of non-NaN elements
% in each column. For N-D arrays, NANSUM operates along the first
% non-singleton dimension.
%
% Y = NANSUM(X,DIM) takes the sum along dimension DIM of X.
%
% See also SUM, NANMEAN, NANVAR, NANSTD, NANMIN, NANMAX, NANMEDIAN.
% Copyright 1993-2004 The MathWorks, Inc.
% $Revision: 2.10.2.4 $ $Date: 2004/07/28 04:38:44 $
% Find NaNs and set them to zero. Then sum up non-NaNs. Cols of all NaNs
% will return zero.
x(isnan(x)) = 0;
if nargin == 1 % let sum figure out which dimension to work along
y = sum(x);
else % work along the explicitly given dimension
y = sum(x,dim);
end
nanmean.mfunction m = nanmean(x,dim)
%NANMEAN Mean value, ignoring NaNs.
% M = NANMEAN(X) returns the sample mean of X, treating NaNs as missing
% values. For vector input, M is the mean value of the non-NaN elements
% in X. For matrix input, M is a row vector containing the mean value of
% non-NaN elements in each column. For N-D arrays, NANMEAN operates
% along the first non-singleton dimension.
%
% NANMEAN(X,DIM) takes the mean along dimension DIM of X.
%
% See also MEAN, NANMEDIAN, NANSTD, NANVAR, NANMIN, NANMAX, NANSUM.
% Copyright 1993-2004 The MathWorks, Inc.
% $Revision: 2.13.4.3 $ $Date: 2004/07/28 04:38:41 $
% Find NaNs and set them to zero
nans = isnan(x);
x(nans) = 0;
if nargin == 1 % let sum deal with figuring out which dimension to use
% Count up non-NaNs.
n = sum(~nans);
n(n==0) = NaN; % prevent divideByZero warnings
% Sum up non-NaNs, and divide by the number of non-NaNs.
m = sum(x) ./ n;
else
% Count up non-NaNs.
n = sum(~nans,dim);
n(n==0) = NaN; % prevent divideByZero warnings
% Sum up non-NaNs, and divide by the number of non-NaNs.
m = sum(x,dim) ./ n;
end追问: 谢谢你!请问怎么安装工具箱呢?我百度了一下好多工具箱啊……这个具体属于哪个的工具箱呢?麻烦你了
我搜索到应该是统计学的工具箱,安装后help语句中出现了这个函数,可以在运算的时候还是有问题啊
追答:你是说help能查到,但运算时报错?那有可能是财经工具箱(Financial Toolbox)的同名函数,但那个函数是对fints对象重载的。你试试执行命令which nansum -all然后贴出结果来。
追问: 谢谢你啦……最后我重新下载安装了2012a就可以运行了。麻烦你了!
00分享举报