Please compile the function file function a=test(b,c,d), so that when the input parameters are 1~3, the output parameter is the mean value of the input parameters (a=b or a=(b+c)/2 or a =(b+c+d)/3). Please directly use the source code of the function
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Please compile the function file function a=test(b,c,d), so that when the input parameters are 1~3, the output parameter is the mean value of the input parameters (a=b or a=(b+c)/2 or a =(b+c+d)/3). Please directly use the source code of the function file created in matlab to answer
function mean = average(a,b,c)
if nargin == 1, average = a
end
if nargin == 2, average = (a+b)/2
end
if nargin == 3, average = (a+b+c)/3
end
end
hello everybody i need ur help to fix this code and can u help how to enter this code into command window need some help thank u
0 Kommentare
Antworten (1)
Ameer Hamza
am 2 Dez. 2020
You cannot define a function in the command window. You need to create a file average.m and save the code inside it. Following shows the correct code
function mean = average(a,b,c)
if nargin == 1
mean = a
end
if nargin == 2
mean = (a+b)/2
end
if nargin == 3
mean = (a+b+c)/3
end
end
Note that I have replaced 'average' with 'mean' inside the function definition. Read about MATLAB functions: https://www.mathworks.com/help/matlab/ref/function.html
0 Kommentare
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!