# How do i remove an outlier?

3 views (last 30 days)
Wouter on 11 Jan 2023
Edited: Wouter on 11 Jan 2023
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
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;
Dyuman Joshi on 11 Jan 2023
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

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 CommentsShowHide 1 older comment
Wouter on 11 Jan 2023
Edited: Wouter on 11 Jan 2023
Thank you kind stranger! It worked!

### Categories

Find more on Logical in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!