Any help would be great! Thanks MATLAB comes with a built-in function mean. In s
ID: 3549166 • Letter: A
Question
Any help would be great! Thanks
MATLAB comes with a built-in function mean. In science application, we often need to compute weighted arithmetic mean (wam) of numbers in the array x. The function is defined as where xk (k=1:n) is the variable value and wk (k=1:n) is the weight. Write MATLAB function y=wam(w,x), which returns the weighted arithmetic mean. Given an example of how your function is used. Your program should return error messages if the lengths of array x and w are different or if any value of w is negative. (There is no need to use loops in this problem.)Explanation / Answer
%Sample input
%x=[1 2 3]
%w=[1 1 1]
%output=2
function [output] = wam(w,x)
size_w=size(w,2);
size_x=size(x,2);
if(isempty(find(w<0))==0)
disp('w contains negative values');
end
if(size_w~=size_x)
disp('Error in size');
else
numerator=x*w';
denominator=sum(w);
output=numerator/denominator;
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.