Create a simple function of one argument, returning two values . A third type of
ID: 2085897 • Letter: C
Question
Create a simple function of one argument, returning two values. A third type of function returns a value (or values) to the command interface or program calling the function. A function definition line is provided below:
function [N, ave] = average(input)
Create a function whose input is a vector and outputs include the number of elements in the input (in variable N) and the average of the input vector (in variable ave). You should be able to perform this operation using only two command lines. The function “length” and “sum” are helpful. When testing your function, type the following at the command window to call your function: [N, ave] = average ([1 2 3 4])
Explanation / Answer
Solution :
We can create the MATLAB function as :
%***************************************************
function [N,ave] = average(input)
N = length(input) ;%returns the lengths of vector a
ave = sum(input)/N ;%define average as sum of elements of "a" divided by total number of elements in "a"
end
%****************
If we run this we will get :
[N, ave] = average([1 2 3 4])
N =
4
ave=
2.500
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.