Directly addressing the Q?, here's a basic function to do the removal--
function x=thompsontau(x)
[r,c]=size(x);
cols=1:c;
mn=mean(x,'omitnan');
sd=std(x,'omitnan');
n=sum(isfinite(x));
[mx,ix]=max(abs(x-mn));
isOut=mx>fntau(n).*sd;
if any(isOut)
indxOut=sub2ind([r c],ix(isOut),cols(isOut));
x(indxOut)=nan;
end
end
function tau = fntau(n)
tcrit=tinv(1-0.05/2,n-2);
tau=tcrit./sqrt(n)./sqrt(n-2+tcrit.*tcrit).*(n-1);
end
This vectorizes your loop approach for the first case; your mission, should you choose to accept, is to now encapsulate this to do the subsequent tests until any(isOut) is false.
This could be done by calling the above in a loop or calling it recursively inside this function; will leave that as "exercise for the student" (which seems only appropriate for a t-test based algorithm :) )
NB: This operates on the full input array; if there's reason to not operate on some columns of an array, simply pass the subset. Or, as in this case, the first two columns will never have an outlier show up so it doesn't matter much, but for some datasets obviously that might not be so.
But, don't build such "magic constants" into functions that makes them unsuitable for other use as a general practice.
PS: HINT The recursive solution is by far the neater here and only takes inserting three additional code lines, all of which are consecutive, and two of which are Matlab branching keywords. :) (And, actually, the latter two aren't absolutely mandatory but I think it makes the code more legible to include them).
NB2: fntau uses tinv which requires Statistics TB. Lacking that, will have to revert to using a lookup table or finding another replacement for the t-value.
Best Answer