deleteOutliers.m 892 B

1234567891011121314151617181920212223242526272829303132333435
  1. function [ dat_clean, nOutliers, idx ] = deleteOutliers( dat, sdCrit )
  2. % function dat = _deleteOutliers(dat, sdCrit)
  3. % this function returns data vector without outliers.
  4. % It automatically detect outliers by applying M+SD or M-SD threshold,
  5. % making outliers with NaN data format.
  6. if nargin < 2
  7. sdCrit = 3;
  8. end
  9. l = length(dat);
  10. m = nanmean( dat );
  11. sd = nanstd( dat );
  12. dat_clean = dat;
  13. [ idx_upper ] = find( dat > m+ sd*sdCrit );
  14. [ idx_lower ] = find( dat < m- sd*sdCrit );
  15. idx = [ idx_lower; idx_upper ]';
  16. if min(size(idx)) > 1 % Matrix orientation
  17. idx = [idx_lower, idx_upper ];
  18. end
  19. nOutliers = length(idx);
  20. % disp([ 'hb_deleteOutliers.m >> Outlier N : ' num2str(nOutliers) ...
  21. % ' out of ' num2str( l )...
  22. % ' (' num2str( nOutliers/l*100 ) ' %) ']);
  23. if nOutliers > 0
  24. for oIdx = idx
  25. dat_clean(oIdx) = nan;
  26. end
  27. end