View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

5.0 | 6 ratings Rate this file 12 Downloads (last 30 days) File Size: 12.5 KB File ID: #29887 Version: 1.0
image thumbnail



Jan Simon (view profile)


Gradient of arrays as fast C-Mex

| Watch this File

File Information

Gradient along a specified dimension

Y = DGradient(X, Dim, Spacing, Method)
  X: Real DOUBLE array, any number of dimensions.
  Spacing: Scalar or vector of the length SIZE(X, Dim).
  Dim: Dimension to operate on.
       Optional, default: 1st non-singelton dimension.
  Method: String, order of the applied method for unevenly spaced X:
       '1stOrder', faster centered differences as in Matlab's GRADIENT.
       '2ndOrder', 2nd order accurate centered differences.
       On the edges forward and backward difference are used.
       Optional, default: '1stOrder'.

  Y: Gradient of X, same size as X.

There are a lot of other derivation tools in the FEX. This function is faster (e.g. 25% faster than dqdt and 10 to 16 times faster than Matlab's GRADIENT), works with multi-dim arrays, on a specific dimension only, and can use a 2nd order method for unevenly spaced data.
For vectors or evenly spaced data no temporary memory is needed. Otherwise 1 or 3 temporary vectors of SIZE(X, Dim) are used for the 1st and 2nd order methods.

The C-Mex file must be compiled at first. Pre-compiled files for Win32 can be downloaded at:
Run the unit-test function uTest_DGradient to check the integrity and measure speed.

Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008
Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit

Thanks to Robert A. Canfield (central_diff, #12) and Darren Rowland (diffxy, #29312).


Central Diff.M, Derivative: Central Difference Approximation, Movingslope, Derivative, and Diffxy inspired this file.

This file inspired H2dblab.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
23 Aug 2015 Veronika

In X, I use the default for Spacing. But I think that it is not a problem with the input, after a restart and freeing up some memory I don't get the same problem anymore. Which would also explain why I wasn't able to reproduce the error on a smaller matrix.

Comment only
22 Aug 2015 Jan Simon

Jan Simon (view profile)

@Veronika: Do the NaNs occur in X or in Spacing?

Comment only
20 Aug 2015 Veronika

@Jan: That must have been it, I missed that part, sorry, and also for the very late response. In any case, I was able to compile it by removing the comments and have been using it ever since. Thanks!

I do get problems (Matlab crashes) if my input contains NaNs (which it shouldn't, so I check this before using DGradient). Unfortunately I cannot reproduce this error on a smaller example, though.

02 Jul 2015 Jan Simon

Jan Simon (view profile)

@Veronika: Did you follow the instructions for a compilation under Linux?

Consider C99 comments on Linux:
mex -O CFLAGS="\$CFLAGS -std=c99" DGradient.c

I'm going to post a newer version soon including an installation routine.

Comment only
01 Jul 2015 Veronika

Worked like a charm on Win7 x64 R2014B with Microsoft Visual C++ 2013 Professional.

On Linux with gcc 4.4.6 I am getting many errors about "expected expression before `/' token". I suspect it has to do with the comments - I am trying to remove those to be able to compile the code, but would be nice if there was a better solution.

Comment only
02 Feb 2015 Jan Simon

Jan Simon (view profile)

@mehrnaz baradaran: The C-file must be compiled before it can be used, as described in the help section. If you do not want to install a compiler, you can download a pre-compiled MEX-file from the mentioned download page. Then only the MSVC-distributables might be needed, if you did not install them with any other software before.

Comment only
31 Jan 2015 mehrnaz baradaran

how could i use this file?
it conclude 3 file 2 of them is fuction and the other one doesnt run

Comment only
09 Oct 2013 Yair Altman

Yair Altman (view profile)

Yet another excellent utility by Matlab veteran Jan Simon. Works splendidly out-of-the-box on my Win7 x64 R2013b with Microsoft SDK 7.1, achieving a x15 speedup over gradient() in my specific case.

I expect the speedup to be even higher in multi-dimensional cases, due to DGradient's smart avoidance of calculating unnecessary dimensions (unlike gradient).

The addition of multi-threading and AVX/SSE2 support, which are listed in the utility's TODO list, promise even higher speedups.

I liked the C source-code: it is well documented and highly optimized for speed (e.g., use of register variables, efficient loops and code branches). Anyone who wishes to learn how to write efficient Mex code would do well to investigate this source code.

One minor issue: the internal documentation still mentions dim as the second input arg and Spacing as the third, but I think it should be the reverse. In practice, DGradient(data,spacing) works well.

17 May 2013 SN

SN (view profile)

Great Job! thanks.

12 Jul 2012 Heinrich

Heinrich (view profile)

31 Jul 2011 Zhijun Zhang

This is an awesome code, I have used this function in my project to replace the Matlab gradient function to calculate 2D and 3D image gradient. The executing time is increased by 16 times than 'gradient' function. Thanks Simon!

10 Jun 2011 G. Sken

G. Sken (view profile)

much faster than matlab gradient() !

28 May 2011 Jan Simon

Jan Simon (view profile)

The order of arguments is DGradient(X, Spacing, Dim, Method) in opposite to the documentation. The submission is updated soon.

Comment only

Contact us