how to calculate the distance between 1 and its right neighbour (ε1) in matlab

If you have a vector v and want to calculate the distance between each element and its corresponding right neighbor, you can use the diff function in MATLAB:

main.m
v = [1, 4, 7, 3, 2];
d = abs(diff(v))
38 chars
3 lines

Here, v represents your vector and d is another vector that contains the absolute difference between each element and its corresponding right neighbor. In this case, the output would be [3, 3, 4, 1], representing the distances between 1 and 4, 4 and 7, 7 and 3, and 3 and 2.

related categories

gistlibby LogSnag