Differentiation along horizontal?

6 Ansichten (letzte 30 Tage)
Gregor Lees
Gregor Lees am 15 Mär. 2016
Kommentiert: John D'Errico am 15 Mär. 2016
I just wanted to check if this is a way of computing the difference between element along each element in adjacent columns in a matrix. I have a 480x640 matrix called "A". So the "diff" function gives the difference in for elements in adjacent rows for each column in a matrix? So to get the difference between each element in the adjacent columns can I take the transpose of that original matrix "A" making it "Atranspose" then perform the "diff" function and then take the transpose of that differentiated matrix therefore brining me back to just differentiated A?

Antworten (2)

Image Analyst
Image Analyst am 15 Mär. 2016
In the documentation for diff, you can see it has a "dim" argument, like many, many MATLAB functions:
Y = diff(X,n,dim) is the nth difference calculated along the dimension specified by dim. The dim input is a positive integer scalar.
dim=2 would mean differences between columns.

John D'Errico
John D'Errico am 15 Mär. 2016
Bearbeitet: John D'Errico am 15 Mär. 2016
There is no need to transpose the matrix. Just tell diff what dimension on which to operate. Look at the third input argument of diff.
Yes, you could compute that difference by a pair of transposes, but why bother? Or, what if you have a 3 dimensional array?
diff(magic(3),[],2)
ans =
-7 5
2 2
5 -7
HOWEVER! This is NOT a derivative. It IS only a difference.
There IS a difference between the two, and it may be important. If the stride between columns of the matrix (viewed in real world units) should be some other number than 1, then you would at least want to divide by that stride.
Next, remember that a derivative is approximated as delta_y/delta_x. This is still not a derivative though. It is only an approximation to one.
x = 0:.1:1
x =
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
y = x.^2
y =
0 0.01 0.04 0.09 0.16 0.25 0.36 0.49 0.64 0.81 1
Don't forget to divide by the stride between terms! If that stride is 1, then all is good of course, and diff(y) works just fine on its own.
diff(y)/0.1
ans =
0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9
The true derivative at those points is of course:
2*x
ans =
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
And finally, note that diff produces one less element in the result, because it really is just a successive difference. Think of that derivative estimate as being most accurate midway between teach pair of elements.
  2 Kommentare
Gregor Lees
Gregor Lees am 15 Mär. 2016
cheers for answering. I had assumed that for the matrix the difference and the partial derivative would be the same. Basically I want to take partial x and partial y of the 480x640 matrix. This should leave me with one matrix of 479x640 and 480x639. can i use the diff function to achieve this?
John D'Errico
John D'Errico am 15 Mär. 2016
Of course.
diff(A,[],1)
diff(A,[],2)
do what you desire.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by