1234567891011121314151617181920212223242526272829303132333435 |
- function [ dat_clean, nOutliers, idx ] = deleteOutliers( dat, sdCrit )
- % function dat = _deleteOutliers(dat, sdCrit)
- % this function returns data vector without outliers.
- % It automatically detect outliers by applying M+SD or M-SD threshold,
- % making outliers with NaN data format.
- if nargin < 2
- sdCrit = 3;
- end
- l = length(dat);
- m = nanmean( dat );
- sd = nanstd( dat );
- dat_clean = dat;
- [ idx_upper ] = find( dat > m+ sd*sdCrit );
- [ idx_lower ] = find( dat < m- sd*sdCrit );
- idx = [ idx_lower; idx_upper ]';
- if min(size(idx)) > 1 % Matrix orientation
- idx = [idx_lower, idx_upper ];
- end
- nOutliers = length(idx);
- % disp([ 'hb_deleteOutliers.m >> Outlier N : ' num2str(nOutliers) ...
- % ' out of ' num2str( l )...
- % ' (' num2str( nOutliers/l*100 ) ' %) ']);
- if nOutliers > 0
- for oIdx = idx
- dat_clean(oIdx) = nan;
- end
- end
|