MATLAB Answers

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

6 views (last 30 days)
Joseph Conlon
Joseph Conlon on 19 Mar 2020
Answered: James Tursa on 19 Mar 2020
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?

  0 Comments

Sign in to comment.

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.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products