Using rmoutliers without a for loop
1 view (last 30 days)
Show older comments
Hi!
I am having trouble with removing outliers from an array of Gaussian distributed vectors, with an unequal amount of outliers and different offsets.
e.g.:
% three Gaussian distributed vectors, with unequal outliers and different
% offsets. (b has 2 outliers, a and c only 1)
a = [60,1, 2,3,3,4,4,4,5,5,6,5,5,4,4,4,3,3,2,1];
b = [60,60,2,3,3,4,4,4,5,5,6,5,5,4,4,4,3,3,2,1] + 500;
c = [60,1, 2,3,3,4,4,4,5,5,6,5,5,4,4,4,3,3,2,1] + 1000;
% array of the vectors above
array = horzcat(a',b',c');
% remove outliers
arrayOutliersRemoved = rmoutliers(array)
The code almost works, however, vector a and b have a value removed which is were not outliers. This is because b had two outliers, and therefore it will remove an extra value from a and c so that an array can be made.
I have the code in the form of a for loop know, is there a way to do this without one?
I have tried using a cell array, but the rmoutliers function does not support it.
Thanks in advance!
1 Comment
Voss
on 2 Mar 2023
Please share your code which is in the form of a loop, and please share the intended result (e.g., a cell array of column vectors of different lengths or something else).
Accepted Answer
Simon Chan
on 2 Mar 2023
You may consider using function filloutliers and fillmethod to replace the outliers with a numeric scalar, which is NaN.
More Answers (1)
Les Beckham
on 2 Mar 2023
Edited: Les Beckham
on 2 Mar 2023
From the documentation of rmoutliers:
- If A is a matrix, then rmoutliers detects outliers in each column of A separately and removes the entire row.
One way to do this is to remove the outliers from a, b, and c first (separately) and then combine them into a cell array.
Another possibility, if you want to retain the original size of the array and apply the operation after combining a, b, and c, is this, using filloutliers instead of rmoutliers:
% three Gaussian distributed vectors, with unequal outliers and different
% offsets. (b has 2 outliers, a and c only 1)
a = [60,1, 2,3,3,4,4,4,5,5,6,5,5,4,4,4,3,3,2,1];
b = [60,60,2,3,3,4,4,4,5,5,6,5,5,4,4,4,3,3,2,1] + 500;
c = [60,1, 2,3,3,4,4,4,5,5,6,5,5,4,4,4,3,3,2,1] + 1000;
% array of the vectors above
array = horzcat(a',b',c');
% remove outliers
arrayOutliersRemoved = filloutliers(array, 'nearest') %<<< there are other options besides 'nearest'
I would suggest reading the documentation for this function and see if you can make this work for you.
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!