Weighted mean of n-dimensional array

Hi,
I have two n-dimensional arrays (exactly the same size). One of the arrays (A) represents the mean of some data, and the second array (B) represents the number of samples that the mean values in A were based on.
I now want to aggregate across any one dimension of A, say the nth dimension, similarly to C=mean(A,n), but rather than obtaining the arithmetic mean across dimension n, I want the weighted average based on the values in B.
Is there a neat way to do this? I guess I could do it in a loop, but it turns out my brain doesn't agree with more than 3 dimensions.
Thanks in advance for any advice! Staffan

回答 (2 件)

the cyclist
the cyclist 2017 年 6 月 9 日
編集済み: the cyclist 2017 年 6 月 9 日

0 投票

My best guess is that you are going to want to do something like ...
% Your data
A = rand(3,4,5)
B = rand(3,4,5);
% Calculate a weighted mean
N = 2; % Dimension for the mean
weightedMeanA = sum(A.*B,N)./sum(B,N); % Exact formula here depends on how to weight

2 件のコメント

Staffan Lindahl
Staffan Lindahl 2017 年 6 月 9 日
編集済み: Staffan Lindahl 2017 年 6 月 9 日
Thanks for the prompt answer - I think there was a small error in it, but it helped me solve it. It must simply be:
weightedMeanA = sum(A.*B,n)./sum(B,n);
(which is exactly what one would intuitively do in 2d).
the cyclist
the cyclist 2017 年 6 月 9 日
Yep. I edited my answer to reflect your correction.

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

Tatevik Melkumyan
Tatevik Melkumyan 2018 年 3 月 28 日

0 投票

Hello, I have a 776x1032 matrix , I need to calculate matrix's weighted average and then calculate it's rms size. How can I do it. I'm new in this field and don't know much. Please help me.
Thanks in advance!

1 件のコメント

the cyclist
the cyclist 2018 年 3 月 28 日
編集済み: the cyclist 2018 年 3 月 28 日
I recommend submitting a new question, rather than placing it as an "answer" on a year-old question.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2017 年 6 月 9 日

編集済み:

2018 年 3 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by