# need to make a function to change a high value to the average in a matrice

2 views (last 30 days)
slowlearner on 1 Jun 2020
Commented: Star Strider on 1 Jun 2020
I'm trying to change values that are greater than 450 and change them to the average of the former and the next.
i made a loop for it but don't know how to make it in to function to call the one i made was ok it did what i expected and looks like this:
L = length(SensorValue1) %SensorValue1 is a .txt file with values
Value = reshape(SensorValue1)%just to make it in to a [L 1] matrice
for i = 1:L
if Value(i)>450
Value(i) = (Value(i-1)+Value(i+1))/2;
end
end
i did the import and the loop works in the main script but don't know how to make it into a function to call i thought something like this:
%%outliersRemoverAVG
function outliersRemoverAVG(x)
n = length(x);
% [m n] = x
for i = 1:n
if x(i)>450
x(i) = (x(i-1)+x(i+1))/2;
return
% else
% x(i)
% return
end
end
but it said to many output arguments and don't know how to print the new matrice using the function

Star Strider on 1 Jun 2020
Edited: Star Strider on 1 Jun 2020
The fillmissing function (R2016b and later releases) may be able to do what you want. First, set the values >300 to NaN (you do not need a looop for that), then use the approach described in Interpolate Missing Data to linearly interpolate them. That would likely be the same as taking the mean of the adjacent values.
EDIT —
SensorValue1 = randi([10 400], 75, 1); % Create Vector
x = (1:numel(SensorValue1)).'; % Independent Variable Vector
Copy = SensorValue1; % Copy For Later Plot (Delete)
idx = SensorValue1 > 300; % Logical Index Of Values Meeting Criterion
SensorValue1(idx) = NaN;
[SensorValue1,TF] = fillmissing(SensorValue1, 'linear', 'SamplePoints',x);
figure
plot(x, Copy, 'ob', 'MarkerFaceColor','b')
hold on
plot(x, SensorValue1, ':or')
hold off
grid
legend('Original', 'Interpolated')
producing (with one set of random variables) — slowlearner on 1 Jun 2020
Cheers this works to i think! but if i understand correctly if i were to change :
[SensorValue1,TF] = fillmissing(SensorValue1, 'linear', 'SamplePoints',x);
%with
[SensorValue1,TF] = fillmissing(SensorValue1,'movmedian',3);
this will be the exact thing i needed right? need to try this one thank you!
Star Strider on 1 Jun 2020
As always, my pleasure!
That likely will do what you want. I chose interpolation because I do not have access to your data. Note that the median and the mean are not the same, so experiment with 'movemean' as well.