Problem 106. Weighted average
Solution Stats
Problem Comments

23 Comments
weighted mean should really be sum(x.*w)/sum(w), and not as defined in the problem.
I agree with the definition above. You need to define the sum of all the weights and not the number of weights.
As the others have said, the problem title is flat out wrong. What is required is simply not a weighted average in any standard form.
Come on Mathworks, delete this problem!
The test suits for this problem should have tests with different vector length.
There are "solutions" that just divide by 3.
I think the program title is wrong as in weighted average the denominator should contain sum of all weights
Hahaha, who wrote this problem? This is not a weighted average! Just look at the example provided: the weighted average of 1, 2 and 3 is 33.3333, hahahahaha!
this is not really the weighted average, because of 10 15 and 20 the average can't never be 33.333 here is a bug
Yes. The description is completely wrong. To start with, a weighted average should have the property that
weighted_average(x,w) == weighted_average(x,k*w)
So a simple rescaling of the weights should not impact the result.
Good problem
kinda misleading, but i get the gist of it
good question
yay
tricky
easy
Is it possible to remove this problem or at least change the problem name so it does not say "weighted average". As others have pointed out, the definition, via example, is inaccurate.
An average, weighted or otherwise, will not be greater than the largest number in the set (which is the case here).
In case you actually want to try a weighted average problem, you can check out the one here
https://www.mathworks.com/matlabcentral/cody/problems/44156weightedaverage
very good
nice
why leading size is always 7? and how :(
On a weighted average we should divide the weighted values by the sum of the weights and not by the number of terms. https://en.wikipedia.org/wiki/Weighted_arithmetic_mean
@Mehmed Saad: some of the many cheating solutions have been deleted.
This problem definitely needs to be fixed to use the correct weighted average formula.
Solution Comments

1 Comment
Perfect

1 Comment
Nice problem!

2 Comments
This is NOT the weighted average. The denominator should be the sum of the weights. PLEASE FIX THIS!!!
Daniel is correct.

1 Comment
Nice function!

1 Comment
This WAS the leading solution (not mine), but I removed the regexp function so you can see that it is a terrible solution.

1 Comment
Your weight vector should be a unit vector. W=w/norm(w) to get a true weighted average. Dividing by the max is dumb.

1 Comment
.

1 Comment
keep it up

2 Comments
Nice problem
easy

1 Comment
(^o^)v

1 Comment
This user (mohamed elbesealy) appears to have gamed the system, with fraudulent "likes" of this unremarkable solution submitted by 'sockpuppet' accounts. —DIV

2 Comments
This is a seriously flawed problem. I'm sorry, but it is. It uses an incorrect definition of a weighted average. As such, students might solve this problem, then try to use that same expression in code for some future job. A correct solution might better be:
y = x*w'/sum(w);
Agree the definition of "weighted average" is flawed. There's still a mistake in your line; it should be y = sum(x.*w)/sum(w), or y = dot(x,w)/sum(w).

1 Comment
That's good.

1 Comment
how the heck can you get smaller than?
y = x*w'/3

1 Comment
Sweet.

1 Comment
obviously misuse of poor testing...

1 Comment
Easy, but not the weighted average. Even assuming that the first vector contains the weighting factors (for example the sizes of different sample groups) and the second vector contains the means of the sample groups, than the divisor has to be the sum of the weighting factors instead of the number.

1 Comment
This is beautiful! Smart move!

1 Comment
This is not the weighted average

1 Comment
Good one

1 Comment
odd name

1 Comment
I think the problem is not well formulated: in a weighted average, your weights should add up to one

1 Comment
FP operations and isequal(), sometimes yield some bitter results

1 Comment
l doubt on this definition of the problem

1 Comment
This is not the Weighted average. One should divide by the sum of the weighs, not by the number of them.

1 Comment
This is not the weighted average.
The weighted average has the sum of w in the denominator

2 Comments

1 Comment
This probably shouldn't pass...

1 Comment
I like the commented "y =(x*w.')/length(x)" more than the "shorter" solution.
Problem Recent Solvers6501
Suggested Problems

439 Solvers

Flag largest magnitude swings as they occur
626 Solvers

Solving Quadratic Equations (Version 1)
449 Solvers

612 Solvers

5573 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!