You are making this very hard on yourself! The simplest way to re formulate the innermost loop is
block = y((-1:1)+i, (-1:1)+j);
cleanRGBarray(i,j) = median(block(:));
A couple of things to note:
- You attempted to create a vector of arguments for
median
but didn't surround it with[]
- I used the fact that when indexing over more than one dimension, Matlab does "nested loops" to get all the numbers (so my
block
variable ends up being 3x3) - I feed a column vector
block(:)
with dimensions9x1
intomedian
- if you give it an N dimensional matrix, it will operate on the first non-singleton dimension only (so give it 3x3 and it returns 1x3 medians) - There are techniques for doing this much more efficiently - built in median filters, and things like
blockproc
- but you need the Image Processing Toolbox for those.
I hope this helps a bit.