Can I write a script that keeps rows that are within 5% and 10 cells above and below a specific cell?

1 view (last 30 days)
The idea is to have a script that takes stable data eleminating the peaks and valleys.
I want to have that cell compared with the 10 before and after itself and if all this 21 values are within 5% then I can keep that data. That script should also be able to check all the rows of an excel spreadsheet.
Any suggestions and ideas are much appreciated! I am a newby trying to do advanced stuff :)

Accepted Answer

Rik
Rik on 21 Aug 2019
If you have a cell array you should convert it to a numeric type (e.g. a double).
You can find outliers by using the movmean function:
k=21;
outlier=abs((data./movmean(data, k))-1)>=0.05;
Now you have a logical array with the outliers. Do you want to remove them or replace them?
  8 Comments
Diego Dranuta
Diego Dranuta on 23 Aug 2019
This excel sheet is representative of what I am trying to do. I need to separate those transient regions from those "steady" regions to analyse individually.
My idea to do so is to have a code that separates them by using this rule. If x has a max 5% difference with x-1,x-2,x-3,x-4,x-5,x-6,x-7,x-8,x-9,x-10 and x+1,x+2,x+3,x+4,x+5,x+6,x+7,x+8,x+9,x+10 then it is steady else is transient.
for expample is x-9 is 6% away from x then x is transient
Hope this gives you a better insight of what I am trying to do.
Thank you very much for your kind help!
Rik
Rik on 23 Aug 2019
Edited: Rik on 23 Aug 2019
So for each value within the window x/x_window must be between 0.95 and 1.05? If that is the case you could use something like the code below.
%your data and setting
data=[55;56;54;55;55;55;59;54;55;52;55;56;56;57;50;60;71;75;115;60;50;40;...
23;20;25;30;40;50;55;56;55;44;55;55;55;55;56;54;57;54;54;56;58;54;...
58;56;55;55;55;54;55;55;56;55;55;55;55;55;55;54;54;56;54;54;54;56;...
55;54;58;58;57;58;56;54;55;55;55;55;55;56;55;55;55;54;55;55;55;56;...
55;55;55;54;55;55;55;56;55;55;55;54;55;55;55;56;55;55;55;54;55;55;...
55;55;57;55;55;55;57;55;55;55;56;56;57;56;57;56;58;57;56;55;56;56;...
56;57;57;57;100;90;15;20;4;5;56;47;58;57;21;44;33;66;88;77;44;55;...
66;23;55;54;56;57;58;98;54;2;42;52;5;44;44;56;87;45;15;35;8;55;65;...
45;78;77;66;55;44;54;56;54;58;59;54;56;54;56;54;56;54;54;56;54;56;54];
k=21;
%%
%example data and setting
data=[100 100 100 106 100 98 100];
k=3;
%%
clc
L=false(size(data));
for n=( -(k-1)/2 ):( (k-1)/2 )
if n==0,continue,end
current=circshift(data,n);%shift the vector one position over
ratio=data./current;
p_diff=abs(ratio-1)*100;
L=L | p_diff>5;%store all positions where the ratio is off
end
data(L)=[]
With the example data it is easy to see that a lot of values are getting removed if you do it like this. The reason is that an invalid value 'taints' the surrounding values. In this case the 106 value causes the 100 on either to be removed as well.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!