MWB_iterative_outlierdetection.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function [cleaneddat removedtrl] = MWB_iterative_outlierdetection(dat, sd, minnrtrlremaining)
  2. % dat = input-vector of data
  3. % sd = how many sds are acceptable (default = 3)
  4. % minnrtrlremaining = minimum nr of trials that should be kapt after exclusion
  5. %
  6. % cleaneddat = data after outlier removal
  7. % removedtrl = indices of data-points deleted from dat
  8. if nargin < 2, sd = 3; minnrtrlremaining = 50; end
  9. if nargin < 3, minnrtrlremaining = 50; end
  10. % make sure dat is a coloumn-vector
  11. [tmp.s1 tmp.s2] = size(dat);
  12. if tmp.s2 > tmp.s1; dat = dat'; end
  13. clear tmp;
  14. rundetection = 1; tmp.dat = dat; removedtrl = [];
  15. while rundetection > 0
  16. if length(tmp.dat) < minnrtrlremaining
  17. rundetection = 0;
  18. else
  19. [tmp.q1 tmp.q2 ol] = testboxplot(tmp.dat, sd, 0, 0); % based on an m-file from mathworks-central
  20. if isempty(ol)
  21. rundetection = 0;
  22. else
  23. % determine the indices for trl in dat
  24. for i1 = 1:size(ol,1)
  25. tmp.idx = find(dat == tmp.dat(ol(i1,1)));
  26. removedtrl = cat(1,removedtrl,tmp.idx); tmp = rmfield(tmp,'idx');
  27. end
  28. tmp.dat(ol(:,1)) = [];
  29. end
  30. end
  31. end
  32. cleaneddat = tmp.dat;