discrete numerical 2D gradient

*To*: mathgroup at smc.vnet.net*Subject*: [mg113084] discrete numerical 2D gradient*From*: Sebastian Schmitt <sschmitt at physi.uni-heidelberg.de>*Date*: Tue, 12 Oct 2010 13:49:53 -0400 (EDT)

Dear all! I have 2D data represented as a matrix. I would like to obtain the gradient. I searched quite some time in the Mathematica documentation to find a function that does the job without success. Therefore I came up with something myself. It is more for illustrative purposes than real life application, e.g. I'm taking a simply forward difference. Could you please take a look at my solution? (it should be copy'n'pastable) ------------------------------------------------------------------------ nrows = 10; ncolumns = 10; dataMatrix = Table[i*j, {i, nrows}, {j, ncolumns}]; dataMatrix//MatrixPlot fwdDifferenceMatrix = SparseArray[{Band[{1, 1}] -> 1, Band[{1, 2}] -> -1}, {nrows, ncolumns}, 0] ; fwdDifferenceMatrix//MatrixForm dxMatrix = dataMatrix.fwdDifferenceMatrix; dyMatrix = Transpose[Transpose[dataMatrix].fwdDifferenceMatrix]; dxyMatrix = Function[{u, v}, List[u, v], Listable][dxMatrix, dyMatrix]; subdxyMatrix = dxyMatrix[[2 ;;, 2 ;;]]; Rotate[ListVectorPlot[subdxyMatrix],-90Degree] ------------------------------------------------------------------------ Is there a better (both mathematical and Mathematical) to construct the y-derivatives (dyMatrix)? I had also expected to find a built-in function to combine dxMatrix and dyMatrix to a matrix of vectors. But maybe I just have to accept to use pure function a lot. The last problem I have is that ListVectorPlot has its origin in the lower-left corner while the matrix counts from upper-left to lower-right. I simply rotated the plot. Could I have done something else? Thanks in advance, Sebastian

**Follow-Ups**:**Re: discrete numerical 2D gradient***From:*Mark McClure <mcmcclur@unca.edu>