MATLAB Answers

How to create a weighted average for the following data?

3 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2017 年 4 月 4 日
編集済み: James Tursa 2017 年 4 月 4 日
I am trying to create an average of the 'average' column below located in my Avg structure (Avg.average, Avg.population, etc). However, each 15x15 Avg.average array is not weighted the same, how would I average these to include the weights of the 'population' field (seen below) and output a single 15x15 array?

  0 件のコメント

サインインしてコメントする。

回答 (1 件)

James Tursa
James Tursa 2017 年 4 月 4 日
編集済み: James Tursa 2017 年 4 月 4 日
One way:
result = sum(bsxfun(@times,reshape([Avg.population],1,1,[]),reshape([Avg.average],15,15,[])),3)/sum([Avg.population])
Or in pieces that are easier to follow:
p = [Avg.population];
a = [Avg.average];
p3d = reshape(p,1,1,[]);
a3d = reshape(a,15,15,[]);
result = sum(bsxfun(@times,p3d,a3d),3)/sum(p);

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by