MATLAB: How to remove outliers from 2D array

arrayfilteroutliers

I have been trying to solve a simple problem for a while now and can't seem to succeed other than brute force method.
I have a 2D array. I want to do statistics on it (i.e., compute mean and std dev). However, there are occassionally invalid values in the array (say below threshold1 and above threshold2). I'd like to either replace those values with null's which will make mean and std ignore them or some other method to ignore them.
For instance, consider: a = reshape(rand(100,1),25,4); a(a>0.9) = 10; a(a<0.1) = -10;
I would like to then compute things like: b = mean(a,2);
but exclude the elements > 1 and < 0 in the computation. If I could exclude them, then elements of b would be averages of 0 to 4 numbers.
Using things like: a(a<0.1) = [];
doesn't work because it turns the 2D array into 1D which can't be reshaped back to original format.

Best Answer

Replace invalid values with NaN.
You can then use the function nanmean with the stats toolbox or there is a FEX posting with a similar function.
Good luck