How can I repeat an iteration after if has been verified?

13 views (last 30 days)
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!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 24 Aug 2013
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
  2 Comments
Pieter Kooijman
Pieter Kooijman on 24 Aug 2013
Edited: Pieter Kooijman on 24 Aug 2013
Tnx for the reply, it seems like a good solution, but now the script does not need to rerun the Grubbs test. So 'h' will remain 1 and the if commands will repeat infinitely. Any ideas?
Azzi Abdelmalek
Azzi Abdelmalek on 24 Aug 2013
Try this
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;
if all(isnan(r(g,:)))
g=g+1;
end
else
g=g+1;
end
end

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!