How to scan an array for outlying data and replace it with the average of the good data points surroundingi t

I have a series of arrays that are about 5000 rows. I need to look through each column for chunks of data whose values are well below or above the other in the array and replace them with the average of the two values surrounding the piece. For example if I had B=[1,2,3,100,100,100,3,2,1] I would want to replace the three one hundreds with the average of 3 and 3. How should I structure my code to find and replace all such problems in a large array?

Answers (1)

B = rand(30,1);
B(10:11) = 100;
% Find values outside 3 standard deviations
m = mean(B);
s = std(B);
idx = B < m - 3*s | B > m + 3*s;
% Replace with linear interpolation
B(idx) = interp1(find(~idx),B(~idx),find(idx),'linear');
The point is that you have to choose a way to discriminate effectively. Your example doesn't work with the 3 std criteria (also because 3/9 of the values are outliers!?). You can use a threshold method, a moving average etc...

Categories

Products

Asked:

on 12 Jul 2011

Community Treasure Hunt

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

Start Hunting!