MATLAB Answers

How do I use rmoutliers without using rmoutliers

34 views (last 30 days)
Connor Tamaki
Connor Tamaki on 21 Jan 2021
Commented: Walter Roberson on 22 Jan 2021
I have to do the rmoutliers function for an assignment without using the rmoutliers function. I believe I need to use a while loop, but I am still unfamiliar with the syntax
Essentially for the while loop I want is to check each value in a vector and check to see if it is outside the threshold and an outlier, return the index and replace the value with []
The threshold is based off of the mean +- 3*std of the vector just like the rmoutliers function
This is the exact function I am trying to replicate:
[B,TF] = rmoutliers(___) also returns a logical vector corresponding to the rows or columns of A that were removed.

  5 Comments

Show 2 older comments
Walter Roberson
Walter Roberson on 21 Jan 2021
The threshold is based off of the mean +- 3*std of the vector just like the rmoutliers function
3*std of the original vector? If so then you do not need any loops.
But once those entries are removed, what remains has a new std and there might be entries more than 3*std of the new vector. Do you want to keep going, or do you want to stop after a single pass?
For example
format long g
x = [1 2 3 4 5 100 1e100]
x = 1×7
1 2 3 4 5 100 1e+100
s = std(x)
s =
3.77964473009227e+99
x(end) > 3*s
ans = logical
0
No entries more than 3*std. If you reduced it down to (say) 2.5*std then only the last entry would get removed.
s2 = std(x(1:end-1))
s2 =
39.6253286001096
but then the 100 would be more than 2.5 the std of what remained. Should it be trimmed out too?
(and remember to subtract out the mean, which I did not do here.)
Connor Tamaki
Connor Tamaki on 22 Jan 2021
the outlier detection is iterative with the mean and std being calculated each time. I know i'm supposed to do a while loop, but I don't know which condition to loop with
Walter Roberson
Walter Roberson on 22 Jan 2021
while true
calculate the current standard deviation
determine whether there are any current points
that are beyond the threshold based upon standard deviation
if there are none, exit the loop
otherwise there must be some. Delete them.
end

Sign in to comment.

Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!