Written and maintained by CASRAI Editorial Board
Last updated
diff in MATLAB is two functions under one name. With a numeric array it returns differences between adjacent elements; with a symbolic expression (Symbolic Math Toolbox) it returns the derivative. This guide shows which one you are calling, what size the result has, and the traps that cause most “my values disappeared” and “diff returned 0” questions. Commands and outputs below are taken from MathWorks documentation (the numeric diff page for release R2023b, and the Symbolic Math Toolbox diff page), read 2026-11-07.
What it is used for in research
Researchers use numeric diff to turn a series into its step-to-step changes (for example successive time stamps or successive measurements) and to approximate a derivative from sampled data. They use symbolic diff to differentiate a closed-form expression exactly, for instance when deriving a sensitivity analytically. Both are covered by the searches “matlab diff” and “diff in matlab”.
Numeric versus symbolic diff: a decision table
| Question | Numeric diff |
Symbolic diff |
|---|---|---|
| What you pass in | A numeric array (also datetime, and tables or timetables from R2023a) | A symbolic expression or function, created with syms |
| What you get back | Differences between adjacent elements, one element shorter per order | The derivative as a symbolic expression |
| Extra requirement | None beyond base MATLAB | Symbolic Math Toolbox |
| Is the result a derivative? | No, only differences; divide by the step size to approximate one | Yes, exact |
Install or access (official source only)
Numeric diff is part of base MATLAB. Symbolic diff needs the Symbolic Math Toolbox, a separate MathWorks product. Obtain MATLAB and its toolboxes only from MathWorks or through your institution’s licence. For setup notes see the CASRAI guide to MATLAB installation. The reference pages are diff (MATLAB, R2023b) and diff (Symbolic Math Toolbox).
Step-by-step task
1. Numeric differences of a vector
- Create the data:
X = [1 1 2 3 5 8 13 21]; - Call
Y = diff(X). The result is0 1 1 2 3 5 8, a 1-by-7 vector. - For a second-order difference use
diff(X,n). ForX = [0 5 15 30 50 75 105];,diff(X,2)returns5 5 5 5 5(1-by-5). The documentation definesdiff(X,2)asdiff(diff(X)).
2. Choose the dimension
The syntax Y = diff(X,n,dim) differences along dimension dim, a positive integer scalar. By default diff works along the first non-singleton dimension. For X = [1 3 5; 7 11 13; 17 19 23];, diff(X,1,2) differences across each row and returns [2 2; 4 2; 2 4]. Plain diff(X) on the same matrix differences down the columns instead.
3. Approximate a derivative from samples
- Sample a function on a uniform grid:
h = 0.001; X = -pi:h:pi; f = sin(X); - Divide the differences by the step:
Y = diff(f)/h;approximates cos(X). - Repeat for the second derivative:
Z = diff(Y)/h;approximates -sin(X).
Each application of diff shortens the vector by one element, so Y and Z are no longer the same length as X; plot them against a correspondingly trimmed x-vector.
4. Symbolic derivatives
- Declare the symbols:
syms f(x)thenf(x) = sin(x^2); - Differentiate:
Df = diff(f,x)returns2*x*cos(x^2). - Evaluate:
Df(2)returns4*cos(4);double(Df(2))gives-2.6146. - Higher orders:
syms t; diff(t^6,4)returns360*t^2,diff(t^6,5)returns720*t, anddiff(t^6,6)returns720. - Mixed partial derivatives:
syms x y; diff(x*sin(x*y),x,y)returns2*x*cos(x*y) - x^2*y*sin(x*y). The documentation notes that mixed derivatives are assumed to commute.
The symbolic forms are diff(f), diff(f,n), diff(f,var), diff(f,var,n), diff(f,var1,...,varN) and diff(f,mvar). For symbol basics see syms in MATLAB.
Output and how to read it
Why the result is shorter than the input
Differences are taken between neighbours, so m values yield m – 1 differences. This is the usual cause of the “lost values” question. The documented size rules are:
| Input | Call | Result size |
|---|---|---|
| Vector of length m | diff(X) |
length m – 1 |
| p-by-m matrix | diff(X) |
(p – 1)-by-m |
| p-by-m matrix | diff(X,1,2) |
p-by-(m – 1) |
| 0-by-0 input | diff(X) |
0-by-0 |
| Table or timetable, p-by-m (R2023a and later) | diff(X) |
(p – 1)-by-m; a 1-by-m input gives 0-by-m |
| Any array, three-argument form with n at least size(X,dim) | diff(X,n,dim) |
empty |
For datetime input, diff returns durations: with t = t1:minutes(1.5):t1+minutes(5) the result is a 1-by-3 duration array, each element 00:01:30.
The default-variable trap in symbolic diff
If you do not name a variable, MATLAB picks the default from symvar(f,1). For syms x t; diff(sin(x*t^2)) the default is x and the result is t^2*cos(t^2*x); naming t explicitly gives 2*t*x*cos(t^2*x). A call such as diff(x*y,2) differentiates twice with respect to the default variable x and returns 0. If the answer is unexpectedly zero, name the variable.
Troubleshooting
- Result is one element shorter, or empty. Expected behaviour; see the size table. An empty result means the order n reached or exceeded the length along the chosen dimension.
- Differencing the wrong direction on a matrix. The default is the first non-singleton dimension (down columns for a matrix). Pass
dimas the third argument to difference across rows. - Derivative approximation looks off.
diff(f)/his only an approximation tied to the sampling steph, and it assumes evenly spaced samples. Use a symbolic derivative when you have a closed form. - Symbolic call errors or returns numbers. Confirm the Symbolic Math Toolbox is installed and the input was created with
syms, not as a numeric variable. - Zero from a multivariate expression. See the default-variable trap above.
Free alternatives
| Tool | Equivalent | What its documentation states |
|---|---|---|
| NumPy (Python) | numpy.diff(a, n=1, axis=-1) |
Computes the n-th discrete difference along an axis, out[i] = a[i+1] - a[i]; the output length along the axis is reduced by n. Example: np.diff(np.array([1, 2, 4, 7, 0])) gives [1, 2, 3, -7]. The default axis is the last, whereas MATLAB defaults to the first non-singleton dimension. |
Source: numpy.diff documentation, read 2026-11-07. For a wider comparison of environments see MATLAB vs R; related MATLAB guides include eigenvalues in MATLAB and moving averages in MATLAB.
FAQ
What does diff do in MATLAB?
On a numeric array it returns differences between adjacent elements along the first non-singleton dimension. On a symbolic expression it returns the derivative.
How do I take a second derivative with diff?
Symbolically, diff(f,x,2) or diff(f,2). Numerically, divide by the step twice, as in Z = diff(diff(f)/h)/h, on evenly spaced samples; diff(X,2) alone gives second differences, not a scaled derivative.
Why is the output of diff shorter than my input?
Each order of differencing removes one element along the dimension, so a vector of length m gives m – 1 values.
Sources (official docs)
- MathWorks, diff, MATLAB R2023b documentation, read 2026-11-07.
- MathWorks, diff, Symbolic Math Toolbox documentation, read 2026-11-07.
- NumPy, numpy.diff, read 2026-11-07.







