How do i remove an outlier?

조회 수: 1 (최근 30일)
Wouter
Wouter 2023년 1월 11일
편집: Wouter 2023년 1월 11일
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 doing so, the data gets skewed. You can see that the values have changed
a=sin(0:0.1:100);
c=a;
a(55)=3;
b=movmedian(a,3);
b==c
ans = 1×1001 logical array
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1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 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by