How can I subtract elements in an array from the element before it.

Hi guys,
I want to know how to subtract every element in an array from the element before it, for example element 2-element1 and element 3-element2 and so on.

 Accepted Answer

help diff
DIFF Difference and approximate derivative. DIFF(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]. DIFF(X), for a matrix X, is the matrix of row differences, [X(2:n,:) - X(1:n-1,:)]. DIFF(X), for an N-D array X, is the difference along the first non-singleton dimension of X. DIFF(X,N) is the N-th order difference along the first non-singleton dimension (denote it by DIM). If N >= size(X,DIM), DIFF takes successive differences along the next non-singleton dimension. DIFF(X,N,DIM) is the Nth difference function along dimension DIM. If N >= size(X,DIM), DIFF returns an empty array. Examples: h = .001; x = 0:h:pi; diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x diff((1:10).^2) is 3:2:19 If X = [3 7 5 0 9 2] then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2 9 -7], diff(X,2,2) is the 2nd order difference along the dimension 2, and diff(X,3,2) is the empty matrix. See also GRADIENT, SUM, PROD. Documentation for diff doc diff Other uses of diff codistributed/diff iddata/diff tall/diff datetime/diff sym/diff tireModel/diff duration/diff symbolic/diff umat/diff gpuArray/diff tabular/diff

11 Comments

so for a matrix I simply need to use diff(function name)
Essentially so. You say matrix however, but earlier you said vector. Diff can be applied to any desired dimension of a matrix.
And now you say function name. If a function returns an array of any size, then you can apply diff to that result.
So, yes.
Yes by function name I meant a function which returns an array. Thank you for all the help it is appreciated
magic(5)
ans = 5x5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Difference of pairs of columns:
diff(magic(5),[],2)
ans = 5x4
7 -23 7 7 -18 2 7 2 2 7 7 2 2 7 2 -18 7 7 -23 7
Difference between consecutive rows.
diff(magic(5),[],1)
ans = 4x5
6 -19 6 6 1 -19 1 6 6 6 6 6 6 1 -19 1 6 6 -19 6
That makes it more clear thank you. I have a function which returns an array of the size 639x1. So I need to do row 2-1 and row 3-2 etc until the final row.
Then you can just call diff, wih no other arguments. For example...
primes(20)
ans = 1x8
2 3 5 7 11 13 17 19
So the MATLAB function which returns a list of primes.
diff(primes(20))
ans = 1x7
1 2 2 4 2 4 2
That makes a lot of sense, but why is there is a 2 at the end of the result array if 11 is not being subtracted by anything?
Um, 19-17 == 2. That seems right to me. There are 8 numbers returned by primes in that call. There will be 7 differences.
Right my apologies I didn’t scroll right on my phone so I didn’t see the rest of the numbers, but that all makes perfect sense. Thank you for all the help and guidance.
Hello @Aman Kumar, if this answer solved your problem, please consider accepting the answer.
Accepting the answer indicates that your problem has been solved (which can be helpful to other people in future) and it awards the volunteer with reputation points for helping you.
You can accept only 1 answer for a question, but you can vote for as many answers as you want. Voting an answer also provides reputation points.
Yes I will definitely accept it sorry about that

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!