MATLAB: How to calculate gradient between the currently processed point (x,y) and its neighboring point in one of eight compass direction.

eight compass directiongradientImage Processing Toolboxneighbor

Dear All,
i'm working for make iterative threshold technique. This technique is first modified with calculating gradient between currently proccessed point (x,y) and its neighboring point in one of eight compass direction that can determined by this equation :
Gd(x,y) = |I(x,y)-I(xd,yd)|
where :
(xd,yd) neighbors to (x,y) in direction d, and I(x,y) and I(xd,yd) denote the gray-level values at locations (x,y) and I(xd,yd). Here d is a value denoting one of eight compass direction.
The value of eight compass direction is
north = [2,10,-6];
north_east = [1,9,-7];
east = [0,8,-8];
south_east = [7,15,-1];
south = [6,14,-2];
south_west = [5,13,-3];
west = [4,12,-4];
nort_west = [3,11,-5];
My problem is how to determined d to take a value of eight compass direction, so i can determined the neighbors point?? i'm really new in gradient based eight direction. Can anyone help me, please?? ^_^

Best Answer

[nrow, ncol] = size(I);
for x = 1 : ncol
for y = 1 : nrow
if y < nrow; G0(x,y) = abs(I(x,y) - I(x,y+1)); end %E
if x ~= 1 && y < nrow; G1(x,y) = abs(I(x,y) - I(x-1,y+1)); end %NE
if x ~= 1; G2(x,y) = abs(I(x,y) - I(x-1,y)); end %N
[...]
if x < ncol & y < nrow; G8(x,y) = abs(I(x,y) - I(x+1,y+1)); end %SE
end
end
So now what?