You could use the conv2 function to find out the elements in the neighbours and then calculate the sum directly.
Below is a link for an answer which could be of use to you.
For more info about conv2 refer to the following link
hope this helps!
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!