MATLAB: Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 9-by-9.

error

Iam trying to recreate a median filter and I am getting the above error at the line:
window(windrow,windcol) = img(row+windrow-window+1,col+windcol-window+1);
I am testing the function by calling it using this line: medianfilter(random,3)
where random is a random 4×4 integer array.
To me, it seems like the left side and right side of the assignment are both 1-by-1. See the full code below:
function medianfilter(img,windowsize)
[m,n]=size(img);
x = ones(m,n);
window = zeros(windowsize,windowsize);
for row = 1:1:m
for col = 1:1:n
for windrow = 1:1:windowsize
for windcol = 1:1:windowsize
if((row+windrow-windowsize+1 > 0) && (col+windcol-windowsize+1 > 0) && (row+windrow-windowsize+1 <= m) && (col+windcol-windowsize+1 <= n))
window(windrow,windcol) = img(row+windrow-window+1,col+windcol-window+1);
end
end
end
x(row,col) = median(window,'all');
end
end

Best Answer

window = zeros(windowsize,windowsize);
That is an array, such as 3 x 3.
window(windrow,windcol) = img(row+windrow-window+1,col+windcol-window+1);
Notice the -window+1 part. window is all of the windowsize by windowsize array, so row+windrow-window+1 is an array and so is col+windcol-window+1. So you are indexing img(3x3 array, 3x3 array) and that is not going to fit inside a single scalar location.
Related Question