Global Index (all files) (short | long) | Local Index (files in subdir) (short | long)
out = detrend_NaN(in, lim);
out = detrend_NaN(in); 'in' is an input matrix where the dimension over which the trend is removed is the first array index: in = in(ntime, nspace1, nspace2, ...);
function out = detrend_NaN(in, lim);
if nargin < 2; lim = 3; end;
out = in;
szout = size(out);
ndim = size(szout, 2);
if ndim > 2;
out = reshape(out, [szout(1) prod(szout(2:ndim))]);
end;
for i = 1:prod(szout(2:ndim));
ind = find(~isnan(out(:,i)));
if length(ind) > lim;
out(ind,i) = out(ind,i) - mean(out(ind,i));
ind1 = (ind - mean(ind))/std(ind);
a1 = ind1' * out(ind,i) / (length(ind1)-1);
out(ind,i) = out(ind,i) - a1*ind1;
else
out(ind,i) = NaN;
end
end
out = reshape(out, szout);