MATLAB: How to ignore an index error

indexerrorMATLAB

I have this code and I want to ignore the error when the index exceeds array bounds or when they are 0.
for j=1:N
for i=1:N
u(i,j)=(c(i,j)+u(i-1,j)+u(i+1,j)+u(i,j-1)+u(i,j+1))/4
end
end
I just want to ignore u(0, j), u(N + 1, j), u(i, 0) and u(i, N + 1) or assign them the value 0.
Thank you so much!

Best Answer

H=[0 1 0;1 0 1; 0 1 0]; % filter coefficients
u(2:end-1,2:end-1)=(c(2:end-1,2:end-1)+filter2(H,u,'valid'))/4; % apply filtered u to c