MATLAB: Calculate differences between all values in vector

derivativediff()differenceforlooplagMATLAB

I'm trying to produce some code that will calculate the differences between nearly all values in a vector.
Specifically, say I have a vector [2 3 1 4]
Starting at 2 and moving through the vector, I need to calculate the difference between 2 and 3 (i.e. -1), 2 and 1, 2 and 4, then 3 and 1, 3 and 4, and then 1 and 4. I don't need to calculate the difference between, say, 3 and 2, and I'm only interested in the differences in one direction.
The vector I want to use the code on will be substantially bigger, and I'd like the output stored in a new, single column vector. I figured diff might provide a way forward, but can't see how to implement it specifying a 'lag'. Any help would be appreciated.

Best Answer

my_vct = [2 3 1 4 5 6];
L = numel(my_vct);
tmp_vct = [my_vct(2:end) my_vct(end)];
x = my_vct - tmp_vct;
sol = zeros(L-1,L);
for k=2:L
tmp_vct = [my_vct(k:end) my_vct((L-k+2):end)];
sol(k-1,:) = my_vct - tmp_vct;
end
sol =
-1 2 -3 -1 -1 0
1 -1 -4 -2 0 0
-2 -2 -5 0 0 0
-3 -3 0 0 0 0
-4 0 0 0 0 0