Hey.
I'm trying to remove some spikes in a water current data using nanmean and nanstd.
To do it, i created this script:
Data3=Data2(:,:,5:6); %Data2 is the real data matrix. To don't take risks, i created a copy and work on it.
it=2;
[~,col,page]=size(Data3);
for p=1:page
for c=1:col
for i=it+1:length(Data3)-it;
x=nanmean(Data3(i-it:i+it,c,p));
y=nanstd(Data3(i-it:i+it,c,p));
if Data3(i,c,p)>x+3*y || Data3(i,c,p)<x-3*y;
Data3(i,c,p)=NaN;
end
end
end
end
%clear p c r x y z k row col page it
After running it, i did a plot to check if the original data(Data2) are now diferent from the one with no spikes(Data3)
plot(Data2(:,5,5))
hold
plot(Data3(:,5,1))
Despite this, i realized that they still equivalents and perfectly equals.
Why my code isn't working?
I appreciate any kind of help on it. Thanks to you all. This community is one of the best in the world!

 Akzeptierte Antwort

Star Strider
Star Strider am 21 Mär. 2022

0 Stimmen

Without the data, it is not possible to determine the reason the code does not work.
See if the rmoutliers function (introduced in R2018b) will do what you want.

6 Kommentare

My matlab version is R2016b and i preferred to don't atualize because of my old computer. Is there a way to you copy and put in here the script of this function so i can try out?
Star Strider
Star Strider am 21 Mär. 2022
That is prohibited, and for good reason.
If you have the Signal Processing Toolbox, see if the hampel function (introduced in R2015b) will do what you want.
It is not possible to determine the reason the code is not working without having the data it is used with. It could be that the ‘spikes’ are less than so nothing is replaced.
Oops, thanks for warning me that!
I'll try the hampe1 function.
Besides that, you don't need the exactly same data that i used to test the command.
i created this matrix to test and it didn't work as well
Star Strider
Star Strider am 21 Mär. 2022
I do not understand the indexing. If you want to create moving windows for the mean and standard deviation, the movmean and movstd functions (R2016a) would be easier. Also see the documentation section on Data Smoothing and Outlier Detection.
Alright, Strider. Thank you so much for all helping and advices. I will test all the options that you gave me.
Wish you a good day.
Star Strider
Star Strider am 21 Mär. 2022
My pleasure!
You, too!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by