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

1 visualización (últimos 30 días)
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 comentario
Bjorn Gustavsson
Bjorn Gustavsson el 20 de Mayo de 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.

Iniciar sesión para comentar.

Respuestas (1)

Star Strider
Star Strider el 20 de Mayo de 2022
Use the gradient function instead of diff.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by