Matlab

# The diff Function in MATLAB

We will see how to implement this function to find the differences between vector elements, rows, and columns of a matrix. In this article, you will also learn how to obtain approximate derivatives of a mathematical function.

This will be shown through practical examples with code fragments and images illustrating the different ways of using this function in multiple dimensions and with varying types of vectors and arrays.

## MATLAB diff Function Syntax

d = diff ( x )
d = diff ( x, n )
d = diff ( x, n, dim )

## MATLAB diff Function Description

The diff() function returns in “d” the difference between one element and the text of the input vector or matrix “x”. We operate along a dimension when we call diff with an array as input. So, the result in “d” will be an array of size n in the dimension of n-1 elements over the dimension on which we operate. The dimension we want to operate on is selected using the input “dim”. The input “n” is an integer scalar that sets the order of derivatives. This function accepts vector, 2D, and multidimensional arrays in “x”, while the inputs “n” and “dim” are of positive integer scalar type. We will see some practical examples of this function with vectors and different matrix types below.

## Example 1: How To Get the Differences Between the Adjacent Elements of a Vector With the MATLAB Function diff()

Now, let us see how to use the MATLAB function, diff, to find the differences between the adjacent elements of the vector “v”. To do this, we will create a script and write the following code:

v = [ 1, 2, 4, 7, 11, 7, 4, 2, 1 ];
r = diff ( v )

In the first line of the script, we create the 9-element vector “v”. Then, in the second line of code, we call the diff() function, passing “v” as the input argument. Since we are sending a vector in this case, the input “dim” is not used.

As you can see in the following figure, the command console of the MATLAB environment shows that the output in “d” is a vector of the differences between the connected elements of “v”. You can see that the output vector contains one less element than the input vector. ## Example 2: How To Use the “dim” Input to Operate Along Different Dimensions With MATLAB’s diff() Function

In cases where we work with this function using the “dim” input with different dimensions, the “n” input should not be sent empty since diff() takes “n” in its second input argument. If this input is not used, a 1 should be sent instead, which is the default value.

## Example 3: How To Use the “dim” Input to Operate Along First Dimension With the MATLAB diff Function

Now, let us see how to use the MATLAB function, diff, to find the differences between the adjacent elements of the matrix “m” along its columns or dimension 1. For this purpose, we will create a script and write the following code:

m = magic ( 5 )
r = diff ( m, 1, 1 )

In the first line of the script, we use the magic() function to create a magic square consisting of an array of 5 by 5 elements. In the second line of code, we call the diff() function, sending “m” as the input argument and specifying in the “dim” input that it operates along dimension 1.

The following image shows the command console with the result in “d”. In this case, it is an array of five columns by four rows with the differences between the contiguous elements along dimension 1 of “m”. ## Example 4: How To Use the “dim” Input to Operate Along Second Dimension With the MATLAB diff Function

In this example, we will see how to operate on dimension 2 of the matrix, that is, along its rows. To do this, we use the same code fragment as in the previous example, but this time, we indicate by typing “dim” so that it operates along dimension 2 or the rows of the magic square.

M = magic ( 5 )
r = diff ( m, 1, 2 )

The following image shows the command console with the result in “d”. In this case, it is an array of four rows by five columns with the differences between the contiguous elements along dimension 2 of “m”. ## Example 5: How To Get the Approximate Derivatives in a Function With MATLAB diff()

In this example, we will see how to get the approximate derivative of a sine wave using the diff() function, which we will use to get the difference of y in the interval x, x+h, and then divide it by the interval h. Next, we will see the code and script for this example.

x = 0 : 0.01 : 2*pi% h or Delta x = 0.01
y = sin (x);
d = diff ( y ) / 0.01;
plot ( x ( :, 1 : length ( d ) ) , d,  x ( : , 1 : length ( y ) ) , y )

In the previous code snippet, we first create the time vector “x” from 0 to 2*pi with intervals of 0.01 in “h”. Then, we create the vector “y” with the sine of “x” so they will have the same size. Once the wave has been created, with the diff() function, we will obtain the differences between the elements of the vector “y” in the output “d”. Next, we divide the differences in “d” by “h”, and we will obtain a vector with the derivative of “y”. As we said in the description, the size of the diff() output vector is n-1 elements greater than the input vector, and this occurs every time this function is recursively applied through the input “n” so ” x”, and “d” will no longer have compatible sizes. If we want to represent the wave and its derivative, the size of “d” is incompatible with that of “x”. So, we have to define it by the size of “d”, as shown in the last line of the code. Below, you can see the sine “y” and its approximate derivative “d”. ## Conclusion

This MATLAB article explained how to use the MATLAB diff function to find the difference between adjacent elements of a matrix or vector. To help you understand how to use this resource, we have created a practical example with code fragments and images for each mode and different dimensions in which this function works. We have also seen a description of the structure of the function, the input and output arguments, and the data type that diff() accepts. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information. 