How do i remove an outlier?

1 次查看(过去 30 天)
I have a data set where there is an outlier in the date. I am trying to remove it but the way i am trying is not working. The way i am trying it is by writing that if the next number is so much more higher than the previous it should change it to the previous one. I might not have understood correctly how this works because it is not working. Any help is appreciated!
clear all
close all
clc
load('ACE2-opdracht-2-persoonlijk.mat', 'meetdata');
A = meetdata;
Motortoerental = A (:,1);
Stuurspanning = A (:,2);
Tijd = (0:4000)./50;
figure;
subplot(1,2,1)
plot(Tijd, Motortoerental); hold on
xlabel('Tijd(s)')
ylabel('Toerental(omw/min)')
title('v1');
grid;
subplot(1,2,2)
plot(Tijd, Stuurspanning); hold on
xlabel('Tijd(s)')
ylabel('Stuurspanning(V)')
title('v2');
grid;
Stoorsignaal = 0.02*sin(2*pi*Tijd);
v2_verbeterd = Stuurspanning - Stoorsignaal;
figure;
plot(Tijd,v2_verbeterd);
xlabel('Tijd(s)');
ylabel('Stuurspanning(V)');
grid;
% This is the part where i try to remove the outlier. I include my whole code just to be sure though.
for i = 2:length(Motortoerental)
if ((Motortoerental(i)-Motortoerental(i-1))/0.1)>5
Motortoerental_verbeterd(i) = y(i-1);
end
end
figure;
plot(Tijd,Motortoerental_verbeterd);
xlabel('Tijd(s)')
ylablel('Toerental(omw/min)')
grid;
  3 个评论
Wouter
Wouter 2023-1-11
I must have done something wrong. But when i enter that line of code it doesn't remove the outlier from my plot. Do you have any further suggestions?
Dyuman Joshi
Dyuman Joshi 2023-1-11
What is y here? It has not been defined in the code above
for i = 2:length(Motortoerental)
if ((Motortoerental(i)-Motortoerental(i-1))/0.1)>5
Motortoerental_verbeterd(i) = y(i-1);
end
end

请先登录,再进行评论。

采纳的回答

Constantino Carlos Reyes-Aldasoro
Use a moving filter, in your case, the best would be to replace each value by the median value of a neighbourhood, that is if you have [1 3 2] the median is 2, if you have [1 1000 2] median is also 2, check this code
a=sin(0:0.1:100);
a(55)=3;
plot(a)
b=movmedian(a,3);
plot(b)
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-1-11
In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1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0
nnz(b~=c)
ans = 36
Wouter
Wouter 2023-1-11
编辑:Wouter 2023-1-11
Thank you kind stranger! It worked!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by