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

조회 수: 12 (최근 30일)
Bernd  Ab
Bernd Ab 2016년 1월 11일
댓글: Star Strider 2016년 1월 11일
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