MATLAB: How to get the difference between two numbers in a matrix

for loop

To simplify it, i have a 4×2 matrix with Easting and Northing coordinates respectively in each coloum.
A = [20 40
30 60
25 50
10 70]
I want to find the difference in Easting (coloum 1) between the first one and then all the consecutive Eastings after it, and then move onto the next easting, and do the same process again (excluding the all the values above it). This is the same for the Northings (coloum 2).
The difference is found by taking the 'second' value and minusing it from the 'first' value.
Therefore i will get a 6×2 matrix like,
B = [ 10 20
5 10
-10 30
-5 -10
-20 10
-15 20]
I need to understand how to do this so I can find the bearing and distance between each coordinate.

Best Answer

>> A = [20,40;30,60;25,50;10,70]
A =
20 40
30 60
25 50
10 70
>> N = size(A,1);
>> V = 1:N-1;
>> F = @(r)bsxfun(@minus,A(r+1:N,:),A(r,:));
>> M = cell2mat(arrayfun(F,V(:),'uni',0))
M =
10 20
5 10
-10 30
-5 -10
-20 10
-15 20