How do I remove outliers from a matrix?

12 views (last 30 days)
Ahmad Musa
Ahmad Musa on 3 Aug 2016
Answered: Thorsten on 3 Aug 2016
I want to remove outliers from a 3000x3 matrix where each of the 3 columns has a different type of data. I want to remove observations that are different from the mean/median by 3 standard deviations in each column. However, if there is an outlier at (400,1), for example, then I want (400,2) and (400,3) to also be removed regardless of whether they are outliers or not- i.e. if there is an outlier detected then the entire ROW is removed.
Any ideas on how to make a simple script to do this ?
Thanks

Answers (2)

Thorsten
Thorsten on 3 Aug 2016
idx = bsxfun(@gt, R, mean(R) + std(R)) | bsxfun(@lt, R, mean(R) - std(R));
idx = any(idx, 2);
R(idx, :) = [];

KSSV
KSSV on 3 Aug 2016
If you know the row index you can remove the complete row using data(idx,:) = [] ;
Eg:
data = rand(10,3) ;
idx = find(data(:,1)>0.5) ; % from first column get values greater then 0.5
data(idx,:) = [] ; % removes the rows idx from data

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!