MATLAB: Detect Outlier with for Schleife

for outliers

I want to detect Outliers in my Measurement datas and thats why i use Grubbs Outlier Test. I wrote code but i got this error. Could you please help me to solve this problem? Error :Attempted to access a(18); index out of bounds because numel(a)=17.
Error in ausreisser (line 9) PG=abs((a(ii)-mu))/s
clear all
clc
a=[ 1 2 3 4 5 6 7 8 9 10 1100000000000 11 12 90 100 112 1000 1000000000];
mu=mean(a)
s=std(a)
for ii=1:length(a)
PG=abs((a(ii)-mu))/s
VG=3.2095;
else
if VG>PG
disp('kein Ausreisser vor')
disp('Es liegt ein Ausreisser vor')
a(ii)=[];
ii=ii+1
end
end

Best Answer

Try this vectorized approach:
a=[ 1 2 3 4 5 6 7 8 9 10 1100000000000 11 12 90 100 112 1000 1000000000];
mu=mean(a)
s=std(a)
PG=abs(a-mu)/s
VG=3.2095;
outliers = VG > PG
a(outliers) = []
Related Question