개발자 뺚

[Cody] Problem 106. Weighted average 본문

Solution/MATLAB

[Cody] Problem 106. Weighted average

2023. 8. 22. 21:00
Given two lists of numbers, determine the weighted average as follows
Example
[1 2 3] and [10 15 20]
should result in
33.3333 (1*10 + 2*15 + 3*20)/3

function y = weighted_average(x,w)

  [row, col] = size(x);
  
  sum = 0;
  for i = 1:col
      sum = sum + x(i) * w(i);
  end
      
  y = sum / col;
end