This is a bit more of a general question, but, no matter how many times I read the description of MATLAB's im2col function, I cannot fully understand it. I need it for the computational efficiency because MATLAB is awful with nested for loops. Here's what I'm attempting to do, but using nested for loops:
 [TRIMMED]=TM_FILTER(IMAGE, FILTER_SIZE, PERCENT)
    Takes a 2-D array and returns the array, filtered with a
    square trimed mean filter with length/width equal to FILTER_SIZE and percent equal to PERCENT.
    %}
    function [trimmed]=tm_filter(image, filter_size, percent)
    if rem(filter_size, 2)==0                            %make sure filter has a center pixel
        error('filter size must be odd numbered');       %error and return if number is odd
        return 
    end
    if percent > 100 || percent < 0
        error('Percentage must be ? [0, 100]');
        return
    end
    [rows, columns]=size(image);              %figure out pixels needed
    n=(filter_size-1)/2;                      %n is pixel distance from center pixel to boundaries  
    padded=(padarray(image, [n,n],128));      %padding on boundaries so center pixel always has neighborhood
for i=1+n:rows                            %rows from first non-padded entry to last nonpadded entry
    for j=1+n:columns                     %colums from first non-padded entry to last nonpadded entry
    subimage=padded(i-n:i+n,j-n:j+n);     %neighborhood same size as filter
    average=trimmean(trimmean(subimage, percent), percent);         %computes trimmed mean of neighborhood as trimmed mean of vector of trimmed means
    trimmed(i-n, j-n)=average;         %stores averaged pixel in new array
    end
end
trimmed=uint8(trimmed);             %converts image to gray levels from 0-255
 
     
     
    