How can I find the difference between values in an array with an index spacing of 2?

3 views (last 30 days)
Apologies for the basic question.
If I had x=[0,5,4,3,7,9,2,4,7,9]
And I wanted to find the difference between values with a spacing of 2:
I.e. the difference between 0 and 4, 5 and 3, 4 and 7, 3 and 9, 7 and 2 etc...
Returning [4,-2,3,6,-5,-5,5,5] ... How would I go about doing that?

Answers (1)

James Tursa
James Tursa on 19 Mar 2020
Hint: Look at x(1:2:end) and x(2:2:end). If x has an even number of elements, you could reshape(x,2,[]) and then look at some of the diff( ) options.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!