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

조회 수: 1 (최근 30일)
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
Star Strider 2020년 6월 1일
편집: Star Strider 2020년 6월 1일
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 —
Added this example —
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) —
  댓글 수: 2
slowlearner
slowlearner 2020년 6월 1일
편집: slowlearner 2020년 6월 1일
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
Star Strider 2020년 6월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by