Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day96#100DaysChallenge- Matlab Loops |Weighted Average-Matlab Cody

#Day96-Weighted Average-Matlab Cody

Task:

Write a code to find weighted average of given two vectors

 x = [0 -2 3];
w = [10 0 10];
y_correct = 10;

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

function y = weighted_average(x,w)

xlen=length(x);

wlen=length(w);

if xlen==wlen

        sum_xw=0;

        for i=1:1:xlen

                sum_xw=(x(i)*w(i))+sum_xw;

        end

                y=sum_xw/xlen;

else

    fprintf('The lengths are not same');

end

   

end

Sample Input and Output

x = [1 2 3];

w = [10 15 20];

y= 100/3;

Click Here for Video description





Live Support: t.me/matlabirawen



 


No comments

Popular Posts

Followers