Global Index (short | long) | Local contents | Local Index (short | long)
y = detrend_NaN(xdat, tol, show);
function Y = detrend_NaN ( Xdat , tol , show ) ; Y = detrend(Xdat) removes the best linear fit to each column of Xdat. If Xdat is N-dimensional, then it is assumed that the time series Xtim will be removed from the first dimension of Xdat. Missing points are ignored, and the data are assumed to be uniformly spaced in time. tol is the number of points required for a trend to be computed on an individual column of xdat: tol <= size(find(~isnan(xdat(:,i)))). show = 'show', 'noshow', or 1, 0, respectively. This routine is quicker than detrend_NaN and produces the same result, within round-off error.
function y = detrend_NaN(xdat, tol, show); sz = size(xdat); ndim = length(sz); if (ndim == 2) & (sz(1) == 1); xdat = xdat(:); end; sz = size(xdat); ndim = length(sz); if nargin < 3; tol = 2; end; if nargin < 4; show = 0; end; if isstr(show); show = strcmp(show, 'show'); end [m1, n1] = size(xdat); xtim = [[1:m1]' ones(m1, 1)]; [m2, n2] = size(xtim); % Condense data to get rid of NaN's in Xtim if n2 > 1; kp_time = find(~isnan(sum(xtim'))); else kp_time = find(~isnan(xtim)); end nkp_time = length(kp_time); xdat = xdat(kp_time,:); xtim = xtim(kp_time,:); % Start regressions and removals if show; disp(['Number of iterations: ' num2str(n1)]); end; c = repmat(NaN, [nkp_time, n1]); for i = 1:n1; if show; disp(['Iteration ' num2str(i) ' of ' num2str(n1)]); end; if n2 > 1; kp = find(sum( ~isnan((xdat(:,i)*ones(1,n2)) .* xtim)') == n2 ); else kp = find(~isnan(xdat(:,i) .* xtim)); end nkp = length(kp); if nkp > tol c(kp,i) = xdat(kp,i) - xtim(kp,:)*(xtim(kp,:)\xdat(kp,i)); end end % Reshape output so it is the same dimension as input y = repmat(NaN, [m1, n1]); y(kp_time,:) = c; if ndim > 2; y = reshape(y, sz); end