How to overcome the problem with the function diff used to approximate matrix derivative.

1 view (last 30 days)
Hi, I have an issue using diff function to calculate the derivative of a matrix.
Using diff(X), where X is a matrix m x n matrix, I obtain in output a matrix with m-1 rows. The problem is that I need to concatenate the result of the derivative operation with the X matrix along the columns. In other words, I need to obtain a m x 2n matrix where the first n columns are referring to the X matrix and the last n columns are referring to the derivatives of X's columns along the rows.
How can I deal with this problem? Is it rigth to add a row of zeros before applying the diff function or after applying it in order to have the same numebr of rows before concatenating?
  1 Comment
Bjorn Gustavsson
Bjorn Gustavsson on 20 May 2022
For this you have to decide what your application of this requires. What are you trying to achieve? what will you use that final matrix for? Maybe you can use what you suggested, maybe you could use the gradient function instead and have central differences in the main part and single-sided differences along the edge. Only you can decide, since only you know what the use-case is.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 20 May 2022
Use the gradient function instead of diff.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!