Search code examples
matlabweighted-average

Matlab find x of weighted average


How to find the weighted average x? From graphing x(y) it looks to be around x=0.45?

y = [0.1 0.1 0.2 0.5 0.4 0.2]
x = [0.1 0.2 0.3 0.4 0.5 0.6]

One way to perhaps compute it would to find x where the area under the curve y(x) is half of the area under the whole curve. But how to write that in matlab?


Solution

  • Assuming you intend y to be the weights, and you want to compute the weighted average of x, then the weighted average is simply

    sum(x.*y) / sum(y)