diff

diff(A, [dim])

Finite difference operator of matrix or vector.

Examples

Calculate the difference between consecutive elements in an array:

julia> arr = [1, 4, 6, 9, 12];
julia> diff(arr)
4-element Array{Int64,1}:
 3
 2
 3
 3

This example calculates the difference between consecutive elements in the array arr. The resulting array has one element less than the original array.

Calculate column-wise differences in a matrix:

julia> mat = [1 2 3; 4 5 6; 7 8 9];
julia> diff(mat, dims=1)
2×3 Array{Int64,2}:
 3  3  3
 3  3  3

Here, diff calculates the column-wise differences in the matrix mat along dimension 1. The resulting matrix has one row less than the original matrix.

Calculate row-wise differences in a matrix:

julia> mat = [1 2 3; 4 5 6; 7 8 9];
julia> diff(mat, dims=2)
3×2 Array{Int64,2}:
 1  1
 1  1
 1  1

In this example, diff calculates the row-wise differences in the matrix mat along dimension 2. The resulting matrix has one column less than the original matrix.

Common mistake example:

julia> vec = [1, 2, 3, 4];
julia> diff(vec, dims=2)
ERROR: ArgumentError: dims must be a vector of integers specifying a valid subset of dimensions

In this example, dims is provided as 2, which is not a valid dimension for a 1-dimensional vector. Make sure to provide a valid dimension when using diff with multi-dimensional arrays or matrices.

See Also

Ac_ldiv_B, Ac_ldiv_Bc, Ac_mul_B, Ac_mul_Bc, Ac_rdiv_B, Ac_rdiv_Bc, At_ldiv_B, At_ldiv_Bt, At_mul_B, At_mul_Bt, At_rdiv_B, At_rdiv_Bt, A_ldiv_Bc, A_ldiv_Bt, A_mul_B!, A_mul_Bc, A_mul_Bt, A_rdiv_Bc, A_rdiv_Bt, Bidiagonal, cond, conv2, det, diag, diagind, diagm, diff, eig, eigvals, eigvecs, expm, eye, full, inv, isdiag, ishermitian, isposdef, isposdef!, issym, istril, istriu, logabsdet, logdet, lyap, norm, qrfact, rank, repmat, rot180, rotl90, rotr90, sortrows, sqrtm, SymTridiagonal, trace, Tridiagonal, tril, tril!, triu, triu!, writedlm,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: