function retZ = robustize(inZ)
%function retZ = robustize(inZ)
% 
% Reset the values of any outliers in Z, and make subsequent statistical
% analysis robust.
%
% Input:
%   inZ: Z;
%
% Output:
%   retZ: robust Z.
%
% Dabao Zhang    November 26, 2013
%
% Revised by:
%
% Copyright (c) 2013 by Dabao Zhang.

rmean = trimmean(inZ,50);
rstd = median(abs(inZ-rmean))/normcdf(0.75,0,1);
cutoff = norminv(1-0.01/size(inZ,1),0,1)*rstd;
retZ = inZ;
retZ(retZ<rmean-cutoff) = rmean-cutoff;
retZ(retZ>rmean+cutoff) = rmean+cutoff;
