Using nargin: A Difference/Averaging Function In the following task you're required to write a MATLAB function that find

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Using nargin: A Difference/Averaging Function In the following task you're required to write a MATLAB function that find

Post by answerhappygod »

Using nargin: A Difference/Averaging
Function
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:
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:
if x is the same array x = [1, 3, 6, 9, 4] and the
function has been called WITH the 'av' flag (i.e. Two
(2) input arguments are entered) then your output y will
be:
y = [2, 4.5, 7.5, 6.5] (i.e. the average between each
neighbouring element in the array x).
This process is performed by the following
algorithm:
Do not use the in-built function diff() in your
solution.
Important note: Do not use the keywords 'clear' or
'clc' in your solution
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply