calculate deltas of a vector in matlab

Let's say you have a vector v and want to calculate the deltas (element-wise differences) of its elements, you can use the diff function in Matlab.

Here's an example:

main.m
v = [3 8 4 2 9];
d = diff(v);
30 chars
3 lines

The variable d will now contain the deltas of v. In this particular case, d will be a vector with the following values: [5 -4 -2 7].

Note that the diff function calculates the difference between each element and the next element in the input vector. Therefore, the output vector will always have one less element than the input vector.

I hope this helps!

related categories

gistlibby LogSnag