[SciPy-user] Numerical gradient approximation on matrix
Dimitri D'Or
dimitri.dor at fssintl.com
Mon Aug 1 09:22:55 CDT 2005
> Message: 7
> Date: Fri, 29 Jul 2005 14:42:12 -0400
> From: Alan G Isaac <aisaac at american.edu>
> Subject: Re[2]: [SciPy-user] Numerical gradient approximation on
> matrix
> To: SciPy Users List <scipy-user at scipy.net>
> Message-ID: <Mahogany-0.66.0-1428-20050729-144212.01 at american.edu>
> Content-Type: TEXT/PLAIN; CHARSET=UTF-8
>
> On Fri, 29 Jul 2005, T) guillem at torroja.dmt.upm.es apparently wrote:
> > The difference is that gradient.m computes the gradient
> > and keeps the array's shape:
>
> How's that supposed to work.
> I'm not a Matlab user,
> but the docs say it uses diff.
>
> Cheers,
> Alan Isaac
The Matlab gradient function doesn't use diff. It raises similar but not
identical results to diff. A short example to illustrate this:
Take a matrix A:
A =
9 1 1 3
9 4 2 2
4 8 2 0
9 0 6 7
>> [Fx,Fy]=gradient(A)
Fx =
-8.0000 -4.0000 1.0000 2.0000
-5.0000 -3.5000 -1.0000 0
4.0000 -1.0000 -4.0000 -2.0000
-9.0000 -1.5000 3.5000 1.0000
Fy =
0 3.0000 1.0000 -1.0000
-2.5000 3.5000 0.5000 -1.5000
0 -2.0000 2.0000 2.5000
5.0000 -8.0000 4.0000 7.0000
>> diff(A)
ans =
0 3 1 -1
-5 4 0 -2
5 -8 4 7
>> diff(A,1,2)
ans =
-8 0 2
-5 -2 0
4 -6 -2
-9 6 1
The values for the intermediary cells of dx are computed as
A[i,j]=(A[i+1,j]-A[i-1,j])/(2*H[0]), where H[0] is the spacing between the
columns (equal to 14 by default). Working along the same lines with j as
varying index yields dy.
I've written my own gradient function (see
<http://www.scipy.net/pipermail/scipy-user/attachments/20050801/8000d103/gra
dient.obj>
http://www.scipy.net/pipermail/scipy-user/attachments/20050801/8000d103/grad
ient.obj) that works with matrix of dimensions 1, 2 or 3, but I have
difficulties for a generalization to N dimensions. Does someone have an
idea?
Thanks,
Dimitri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.scipy.net/pipermail/scipy-user/attachments/20050801/346feb73/attachment.htm
More information about the SciPy-user
mailing list