To differentiate matrices in MATLAB, you can use the diff
function along the dimension you want to differentiate. Here's an example:
main.m157 chars9 lines
The diff
function takes three arguments: the matrix to differentiate, the order of the derivative (in this case, 1 since we want the first derivative), and the dimension along which to differentiate.
In the above example, dA_dx
will be a 3x2 matrix containing the differences of each row, and dA_dy
will be a 2x3 matrix containing the differences of each column.
Note that diff
uses a forward difference formula, so the result will have one less element along the specified dimension. If you want to maintain the same matrix size, you can pad the result with NaNs or zeros.
gistlibby LogSnag