Given two lists of numbers, determine the weighted average.

Example

[1 2 3] and [10 15 20]

should result in

33.3333 (1*10 + 2*15 + 3*20)/3

Show
11 older comments

Israel
on 27 Jan 2012

weighted mean should really be sum(x.*w)/sum(w), and not as defined in the problem.

Davide Ferraro
on 27 Jan 2012

I agree with the definition above. You need to define the sum of all the weights and not the number of weights.

John D'Errico
on 17 Feb 2012

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.

William Smith
on 25 Sep 2012

Come on Mathworks, delete this problem!

Eilert
on 14 Jan 2014

The test suits for this problem should have tests with different vector length.
There are "solutions" that just divide by 3.

Sneha
on 30 Oct 2014

I think the program title is wrong as in weighted average the denominator should contain sum of all weights

Vittorio
on 9 Jan 2015

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!

Sergej Pauli
on 24 Apr 2015

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

John D'Errico
on 20 Aug 2016

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 re-scaling of the weights should not impact the result.

Benjamin Re
on 8 Dec 2016

Good problem

Muhammad M Sahari
on 20 Dec 2016

kinda misleading, but i get the gist of it

Gan Wee Ting
on 20 Dec 2016

good question

Adi Zabidi
on 27 Dec 2016

yay

Mohan Shanmugam
on 30 Dec 2016

tricky

1 Comment

Jess Stuart
on 7 Feb 2017 at 20:14

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

fahad aljabal
on 29 Dec 2016

.

1 Comment

TAYALAN SIVALINGAM
on 29 Dec 2016

keep it up

1 Comment

Matteo Raso
on 26 Dec 2016

Nice problem

1 Comment

Yoshimasa Nakatani
on 10 Dec 2016

(^o^)v

1 player likes this solution

1 Comment

John D'Errico
on 20 Aug 2016

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);

1 player likes this solution

1 Comment

Siva Madugula
on 3 Nov 2015

That's good.

1 Comment

Kevin Lamb
on 30 Sep 2015

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

1 Comment

Alexander Wickstrom
on 28 May 2015

Sweet.

1 Comment

Hugo
on 2 Apr 2015

obviously misuse of poor testing...

1 Comment

Christian
on 4 Mar 2015

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

Giorgos Papakonstantinou
on 5 Mar 2014

This is not the weighted average

1 Comment

!ntel
on 8 Oct 2013

Good one

1 Comment

david
on 6 Sep 2013

odd name

1 player likes this solution

1 Comment

Randall Stace Romero Aguilar
on 23 Aug 2013

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

1 Comment

Frequency Domain
on 24 Jul 2013

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

1 Comment

sea knowledge
on 13 Dec 2012

l doubt on this definition of the problem

1 Comment

J-G van der Toorn
on 11 Sep 2012

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

1 Comment

Dimosthenis
on 20 Jul 2012

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

2 Comments

1 Comment

Andrew Davis
on 2 Mar 2012

This probably shouldn't pass...

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
17 players like this problem

17 players like this problem