# Mean of values before and after a specific element

4 vues (au cours des 30 derniers jours)
ennes mulla le 29 Juin 2021
Hi
I have an array of 1 row and 400 colmun, where all elments values are above 1500. However, I have some elements that have values <50 which are wrong measures and I would like to have the mean of the elments before and after the wrong measured data points and replace it in the main array
For intance, element number 17 is below 50 so I want to take the mean of elment 16 and 18 and replace element 17 with the new mean.
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Mohammad Sami le 29 Juin 2021
Modifié(e) : Mohammad Sami le 29 Juin 2021
If you have Matlab version greater then R2017a, you can use filloutliers or you can replace the invalid values with NaN an then use fillmissing. You can use "linear" method to replace the ouliers / missing values, which will essentially take the mean of neighbouring values to fill in the outlier / missing value.
% generate test data
a = randi([1000 2000],1,400);
a(randperm(400,10)) = randi([1 50],1,10);
scatter(1:400,a) i = a<= 50;
b = filloutliers(a,'linear','mean');
scatter(1:400,b,[],i,'filled');
colormap jet; c = a;
c(i) = NaN;
c = fillmissing(c,'linear');
scatter(1:400,c,[],i,'filled');
colormap jet; ##### 1 commentaireAfficher AucuneMasquer Aucune
ennes mulla le 29 Juin 2021
It worked!!! thanks a lot buddy! I truly appreciate your help

Connectez-vous pour commenter.

### Plus de réponses (1)

Mathieu NOE le 29 Juin 2021
hello see code below :
clc
clearvars
% dummy data
x = 1:1:400;
y = 1500 + 50*rand(1,400);
y(40)=35; % first outlier (single value)
y(60:66)=45; % second outliers (multiple values)
yi = y; % keep trace of initial y data (for plot)
all_idx = 1:length(x);
% outlier_idx = abs(y - median(y)) > 2*std(y); % Find outlier idx
outlier_idx = y<50; % Find outlier idx
y(outlier_idx) = []; % remove outlier from dataset
y = interp1(all_idx(~outlier_idx), y, x); % replace outliers by interpolated data
figure(1),plot(x,yi,x,y);
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Red dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!