how to find the differnce between all the neighbouring elements in an array???

i have a 64*64 array i need to find the differnce between all the adjacent elements in the array. for instance, if i take a particular element i need to subtract it from the neighhbouring elemnts

Answers (2)

If you want to add all the differences of the center pixel within it's 8-neighborhood, you can construct a filter and apply it to the image:
F = -ones(3); F(2,2) = 1;
Y = conv2(I, F)

5 Comments

i dont need the sum, i would lik to get all the differences of the center pixel within it's 8-neighborhood
Then see Matt's answer. You specify which direction you want, out of 8 possible directions. You can get them one direction at a time if that's what you want. Just be aware that convolution "flips" the window so the direction is 180 from what you might think.
sorry its not 8 neighbourhood but 4 neighbourhood
Same answer. Use kernels:
[0 -1 0; 0 1 0; 0 0 0]
[0 0 0; 0 1 0; 0 -1 0]
[0 0 0; -1 1 0; 0 0 0]
[0 0 0; 0 1 -1; 0 0 0]
for the 4 different directions.
If you only want the 4 vertical/horizontal neighbours, it's probably easier (and faster??) to use DIFF.

Sign in to comment.

The diff() command will give you horizontal and vertical differences. Convolution can give you differences along any direction, e.g.,
conv2(A,[0 0 0; 0 1 0; 0 0 -1]) %differences between A(i,j) and A(i-1,j-1)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 18 Jan 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!