File Exchange

## deleteoutliers

version 1.1.0.0 (2.54 KB) by
For input vector A, returns a vector B with outliers removed.

Updated 21 Mar 2011

[B, IDX, OUTLIERS] = DELETEOUTLIERS(A, ALPHA, REP)

For input vector A, returns a vector B with outliers (at the significance level alpha) removed. Also, optional output argument idx returns the indices in A of outlier values. Optional output argument outliers returns the outlying values in A.

ALPHA is the significance level for determination of outliers. If not provided, alpha defaults to 0.05.

REP is an optional argument that forces the replacement of removed elements with NaNs to preserve the length of a. (Thanks for the suggestion, Urs.)

This is an iterative implementation of the Grubbs Test that tests one value at a time. In any given iteration, the tested value is either the highest value, or the lowest, and is the value that is furthest from the sample mean. Infinite elements are discarded if rep is 0, or replaced with NaNs if rep is 1 (thanks again, Urs).

Appropriate application of the test requires that data can be reasonably approximated by a normal distribution. For reference, see:
1) "Procedures for Detecting Outlying Observations in Samples," by F.E. Grubbs; Technometrics, 11-1:1--21; Feb., 1969, and
2) _Outliers in Statistical Data_, by V. Barnett and T. Lewis; Wiley Series in Probability and Mathematical Statistics;
John Wiley & Sons; Chichester, 1994.

A good online discussion of the test is also given in NIST's Engineering Statistics Handbook:
http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h.htm

ex:
[B,idx,outliers] = deleteoutliers([1.1 1.3 0.9 1.2 -6.4 1.2 0.94 4.2 1.3 1.0 6.8 1.3 1.2], 0.05)
returns:
B = 1.1000 1.3000 0.9000 1.2000 1.2000 0.9400 1.3000 1.0000 1.3000 1.2000
idx = 5 8 11
outliers = -6.4000 4.2000 6.8000

ex:
B = deleteoutliers([1.1 1.3 0.9 1.2 -6.4 1.2 0.94 4.2 1.3 1.0 6.8 1.3 1.2 Inf 1.2 -Inf 1.1], 0.05, 1)
returns:
B = 1.1000 1.3000 0.9000 1.2000 NaN 1.2000 0.9400 NaN 1.3000 1.0000 NaN 1.3000 1.2000 NaN 1.2000 NaN 1.1000

Written by Brett Shoelson, Ph.D.
brett.shoelson@mathworks.com
9/10/03
Modified 9/23/03 to address suggestions by Urs Schwartz.
Modified 10/08/03 to avoid errors caused by duplicate "maxvals."
(Thanks to Valeri Makarov for modification suggestion.)

### Cite As

Brett Shoelson (2021). deleteoutliers (https://www.mathworks.com/matlabcentral/fileexchange/3961-deleteoutliers), MATLAB Central File Exchange. Retrieved .

Anthony Dave

Ali Hassani

Qiang Fu

Thanks

Josh Saha

Big Thanks! Happy Christmas Brett!

Brett Shoelson

@Akhmad: Yes...it was designed to work on vector data. Brett

Skydriver

Hai Brett,
Let me, it works for a vector data, isn't it?

Zheng Wang

Big Thanks!

Salim Arslan

Great work

Tanmay Rajpathak

Brett Shoelson

@Anna: sorry I missed this for a year! :) REP should be true or false. Default is false. I could have described that more clearly.
Brett

Anna Karlsson

Great

Anna Karlsson

This maybe is clear to everyone else, but what should you have for input on REP if you want to use it?

Nice work....

Brett Shoelson

@Hussein,
This is designed to work on a vector of inputs. If you can cast your 3D points as a vector in some meaningful fashion--as distances from a central point, perhaps?--then it would work.
Brett

Hussein

Can this work on 3d points?

Nathan Orloff

Brett Baker

Thanks Brett! Works great. Love the improvements made to it as well. You saved me a chunk of time! I appreciate the sharing!
-Brett

unfortunately doesnt work for my data which has a trend in it and I dont want to remove it from my data

Marcin

Very good. I compared your results with the one from:
on my data and got the same results. Good work!

Hanna Modin

Thank you for a nice implementation of Grubbs test! If I might suggest an improvement that would be to make the test work with other than vectors, e.g. to remove outliers from each row in a matrix separately

dali kaafar

s b

very useful!!!!

James J. Cai

Great

Torsten Staab

Nice job!

Effendi Widjaja

Urs Schwarz (us)

very nice, brett. a few remarks re OUTPUT: there should be an option to replace outlieres with nans (to keep i/o vecs the same length); re INPUT: the option <ul> shows up in the help but doesn't seem to have a meaning (yet?); re PROCESSING: 1) nans are cut away (why? we don't know what a nan is in any context), 2) +-infs, on the other hand, are not (?).

##### MATLAB Release Compatibility
Created with R13
Compatible with any release
##### Platform Compatibility
Windows macOS Linux