How can I subtract elements in an array from the element before it.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
John D'Errico
on 26 Mar 2024
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
Aman Kumar
on 26 Mar 2024
so for a matrix I simply need to use diff(function name)
John D'Errico
on 27 Mar 2024
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.
Aman Kumar
on 27 Mar 2024
Yes by function name I meant a function which returns an array. Thank you for all the help it is appreciated
John D'Errico
on 27 Mar 2024
Edited: John D'Errico
on 27 Mar 2024
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
Aman Kumar
on 27 Mar 2024
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.
John D'Errico
on 27 Mar 2024
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
Aman Kumar
on 27 Mar 2024
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?
John D'Errico
on 27 Mar 2024
Um, 19-17 == 2. That seems right to me. There are 8 numbers returned by primes in that call. There will be 7 differences.
Aman Kumar
on 27 Mar 2024
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.
Dyuman Joshi
on 27 Mar 2024
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.
Aman Kumar
on 27 Mar 2024
Yes I will definitely accept it sorry about that
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)