MATLAB: Operations on consecutive elements of a vector

avoiding loopsmatrices

Imagine there is a vector C with n elements, c(i). Without using a loop I would like to a have new vector D whose elements are:
d(i)= c(i) if i=1
d(i)= c(i-1)-c(i) if 1<i<n
d(i)= -c(i) if i=n
I do not care about the first and last elements which I could add by concatenation, but generally I am looking for a way to perform mathematical operations on consecutive elements of a vector without using a loop. Is this possible?

Best Answer

If'en I interprets your requirements correctly...
d=[c(1) diff(c(1:end-1)) -c(end)];
Example:
>> c=randi(10,1,5)
c =
9 9 4 7 3
>> d=[c(1) diff(c(1:end-1)) -c(end)]
d =
9 0 -5 3 -3
>>