Is it possible to detect and replace abnormal/wrong numbers in an array?

5 次查看(过去 30 天)
I have a dataset which is an array containing values, and which are plotted in the figure below. A section of the array for the figure is: A = [.... 25.9, 25.9, 26.2, 27, 28, 29, 29.3, 29.6, 3, 30.4, 30.5, 30.4, 30.3, 30.3, ....]; Here is number 3 an abnormality in the array. Is it possible to detect these abnormal numbers and replace them with the average value of the number before and after? So for the number 3 in A I want it to be (29.6+30.4)/2.
figure2.jpg

采纳的回答

Kevin Phung
Kevin Phung 2019-3-20
A = [25.9, 25.9, 26.2, 27, 28, 29, 29.3, 29.6, 3, 30.4, 30.5, 30.4, 30.3, 30.3]
abnorm =mean(A) - 3*std(A); % you can set however many standard deviations to constitute 'abnormal'
n = find(A<abnorm); %locations of abnormals
for i = 1:numel(n)
if or(n(i) == 1, n(i)==numel(A)) %if first or last index, replace with NaN;
A(n(i)) = nan;
else
A(n(i)) = (A(n(i) - 1) + A(n(i) + 1)) / 2;
end
end
A(isnan(A)) = []; %remove the endpoints that are NaN
let me know if this works for you
  7 个评论
Kevin Phung
Kevin Phung 2019-3-20
编辑:Kevin Phung 2019-3-20
happy to help!
edit: if number of lines of code is important to you at all, you can actually condense the first 3 lines to one
n = find(isoutlier(A,'movmedian',5)); %locations of abnormals
for i = 1:numel(n)
if or(n(i) == 1, n(i)==numel(A)) %if first or last index, replace with NaN;
A(n(i)) = nan;
else
A(n(i)) = (A(n(i) - 1) + A(n(i) + 1)) / 2;
end
end
A(isnan(A)) = []; %remove the endpoints that are NaN

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2019-3-20

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by