Using the diff comand with a 3D array to get Partial derivatives

12 次查看(过去 30 天)
Dear all
I have a 3D Matrix with the Size of 167x176x2000 Datapoints. Now i want to compute the Partial derivatives in X and Y direction of this matrix. The derivatives would have to be computed parallel to the XY-plane.
But i am not quite sure how to use the diff command. In the matlab help it is stated: "Y = diff(X,n,dim) is the nth difference calculated along the dimension specified by dim. The dim input is a positive integer scalar."
Lets asume the Matrix has the name 3D_Matrix the new matrix should be named delta_x_Matrix. Would i have to type:
delta_x_Matrix = diff(3D_Matrix,1,1);
Would
delta_x_Matrix = diff(3D_Matrix,1,2);
compute the derivative in Y-Direction?
All the best
And thank you in advance!

回答(1 个)

Star Strider
Star Strider 2016-1-11
I would use the gradient function, not diff, since gradient is specifically designed to do what you want.
  2 个评论
Bernd  Ab
Bernd Ab 2016-1-11
编辑:Bernd Ab 2016-1-11
Hello Star Strider;
Thank you for you answer!
So if i use
[FX,FY] = gradient(3D_Matrix);
I would actually get what i desire?
But it still resdueces the dimension of the resulting matrix. I was actually hoping that i could use : diffxy
for my endeavor later on. But at the moment i do not understand what i have no idea what i need to type for "X" into this function. The info for this function is as follows:
DY = DIFFXY(X,Y) returns the derivative of Y with respect to X using a % pseudo second-order accurate method. DY has the same size as Y.
% DY = DIFFXY(X,Y,DIM) returns the derivative along the DIM-th dimension % of Y. The default is differentiation along the first % non-singleton dimension of Y.
% DY = DIFFXY(X,Y,DIM,N) returns the N-th derivative of Y w.r.t. X. % The default is 1. %
% Y may be an array of any dimension. % X can be any of the following: % - array X with size(X) equal to size(Y) % - vector X with length(X) equal to size(Y,DIM) % - scalar X denotes the spacing increment % DIM and N are both integers, with 1<=DIM<=ndims(Y)
% % DIFFXY has been developed especially to handle unequally spaced data, % and features accurate treatment for end-points.
Thank you
Star Strider
Star Strider 2016-1-11
My pleasure.
The gradient function should not reduce the dimensions of the matrix. I checked it with this code snippet to be sure:
M = randi(9, 5, 6, 7);
[GMx, GMy] = gradient(M);
The ‘GMx’ and ‘GMy’ matrices are the same size as ‘M’ here. If you have unequally-spaced data (the gradient function assumes equally-spaced data), then ‘diffxy’ might be more suitable. I’ve not used it, so I can’t comment on it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by