Question: COULD YOU PLEASE DO THIS IN MATLAB and I need the code for it In the following task you're required to write a MATLAB function

 COULD YOU PLEASE DO THIS IN MATLAB and I need the

COULD YOU PLEASE DO THIS IN MATLAB and I need the code for it

In the following task you're required to write a MATLAB function that finds the difference or average between each element in an array and its neighbour. This function will take in the input x which is an array that contains N numerical values. The output of your function will be an array called y which will contain N-1 values (i.e. there'll be one less element in your output array). There's also a second optional flag input which will change your function from the default difference function (where the current value in the array is subtracted from the next value) to an averaging function (where the current value is added to next in the array and the result is divided by 2) if the characters 'av' are input as the flag. In your function you'll need to use nargin, indexing as well as a for-loop to: Determine how many arguments are input into function (use nargin). Determine if the text flag 'av' has been input into the function to indicate an averaging process (set y to the text 'Incorrect flag' if any other letters or numbers are input as the flag). Iterate through each element of the array x. Either subtract the appropriate values away from each other in the array or take the average of the two neighbouring elements. Store the result of this process in the current index of y. 1 1 1 For example, if x is the array x = [1, 3, 6, 9, 4) and the function has been called WITHOUT the 'av' flag (i.e. only One (1) input argument is entered) then your output y will be: y = [2, 3, 3, -5] (i.e. the difference between each element in the array x) This process is performed by the following algorithm: Subtract 1 from 3 (3-1 = 2) Store 2 in index 1 of y Subtract 3 from 6 (6-3 = 3) Store 3 in index 2 of y Subtract 6 from 9 (9-6 = 3) Store 3 in index 3 of y Subtract 9 from 4 (4-9 = -5) Store -5 in final index 4 of y 1 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!