MATLAB: How to repeat an iteration after if has been verified

for loopifiterationoutlierrepeat

My current script can remove 1 outlier per row of data by performing a grubbs test and removing the maximum if it fails.
I would like the script to be able to rerun a row after it has removed an outlier, to determine if there are more outliers (and remove them if necessary). The Grubbs test used here returns a value 'h' of 0 or 1 for either not finding, or having found an outlier. All outliers are positive in this case.
The script currently replaces all '8's in matrix r, but not the secondary outlier '7.9'.
r = [8 5 5 5 5 5 7.9 5 5 5;5 8 5 5 5 5 5 5 5 5;5 5 8 5 5 5 5 5 5 5;5 5 5 8 5 5 5 5 5 5;5 5 5 5 8 5 5 5 5 5;5 5 5 5 5 8 5 5 5 5;5 5 5 5 5 5 8 5 5 5;5 5 5 5 5 5 5 8 5 5;5 5 5 5 5 5 5 5 8 5;5 5 5 5 5 5 5 5 5 8;];
h = zeros(10,1);
for g = 1:10
h(g,:) = Grubbs(r(g,:),0.05,'right');
if h(g,:) == 1
[value, location] = max(r(g,:));
r(g,location) = NaN;
end
Thanks!

Best Answer

Use a while loop instead a for loop
g=1;
while g <10
h(g,:) = Grubbs(r(g,:),0.05,'right');
if h(g,:) == 1
[value, location] = max(r(g,:));
r(g,location) = NaN;
else
g=g+1;
end
end
Related Question