看看nanstd
(統計工具箱)。
想法是使用nanmean
將數據居中,然後用零代替NaN,最後計算標準偏差。
請參閱下面的nanmean
。
% maximum admissible fraction of missing values
max_miss = 0.6;
[m,n] = size(x);
% replace NaNs with zeros.
inan = find(isnan(x));
x(inan) = zeros(size(inan));
% determine number of available observations on each variable
[i,j] = ind2sub([m,n], inan); % subscripts of missing entries
nans = sparse(i,j,1,m,n); % indicator matrix for missing values
nobs = m - sum(nans);
% set nobs to NaN when there are too few entries to form robust average
minobs = m * (1 - max_miss);
k = find(nobs < minobs);
nobs(k) = NaN;
mx = sum(x) ./ nobs;
請參閱下面的nanstd
。
flag = 1; % default: normalize by nobs-1
% center data
xc = x - repmat(mx, m, 1);
% replace NaNs with zeros in centered data matrix
xc(inan) = zeros(size(inan));
% standard deviation
sx = sqrt(sum(conj(xc).*xc) ./ (nobs-flag));
來源
2011-06-28 08:30:12
Wok
太好了,謝謝。不能相信我錯過了這個功能。本來希望當您瀏覽標準:-)的幫助時,將Matlab鏈接到nanstd。再次感謝一百萬。 – hgus1294
不客氣。 – Wok