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

2 vues (au cours des 30 derniers jours)
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 commentaire
Bjorn Gustavsson
Bjorn Gustavsson le 20 Mai 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.

Connectez-vous pour commenter.

Réponses (1)

Star Strider
Star Strider le 20 Mai 2022
Use the gradient function instead of diff.

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by