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

1 回表示 (過去 30 日間)
slowlearner
slowlearner 2020 年 6 月 1 日
コメント済み: Star Strider 2020 年 6 月 1 日
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 Center および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by