Using rmoutliers without a for loop
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Voss
2023-3-2
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).
采纳的回答
Simon Chan
2023-3-2
You may consider using function filloutliers and fillmethod to replace the outliers with a numeric scalar, which is NaN.
更多回答(1 个)
Les Beckham
2023-3-2
编辑:Les Beckham
2023-3-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!