MATLAB: Index exceeds matrix dimensions. ?? I want to create matrix 3×3 kernel

ii

I want to create matrix 3×3 kernel for evaluate of each kernel in image
for i = 2:r
for j = 2:c
K = IY(((i-1):(i+1)),((j-1):(j+1)));
center = IY(i,j);
B(i,j) = std2(K);
end
end
Index exceeds matrix dimensions.
Error in test2 (line 23)
K = IYdouble(((i-1):(i+1)),((j-1):(j+1)));
Could you show me an example please thank you

Best Answer

Assuming r and c are the number of rows and columns of IY, your loop continues past the size of your matrix, because you are trying to access IY(r+1,c+1). So the solution would be to change 2:r to 2:(r-1) and 2:c to 2:(c-1).
If this doesn't answer your question read this and this to improve the chance on getting the answer you need.