Weighted mean of a 3D matrix

5 ビュー (過去 30 日間)
Meghan Rochford
Meghan Rochford 2017 年 9 月 8 日
編集済み: the cyclist 2017 年 9 月 8 日
Hello
I have a 3D matrix with dimensions 2496x10x13857, representing velocities. The dimensions are time, depth and node ID, repectively. I am trying to calucalte the depth weighted average of this matrix. I have a 10x1 matrix with values ranging from 0 to -1 which are the sigma depths. I have been trying out a number of different ways of doing this but I keep getting errors. My script so far:
u=modS.u;
u_new=sum(u.*(siglay'*-1),3)./sum(siglay'*-1,1);
The above calculates the mean but gives a 2496x10 matrix, which is not what I want. I would like the final matrix to be 2496x13857. I have tried to use permute to see if that works but it doesn't. Does anyone have any suggestions as to how I could go about this?
Thanks in advance :)

採用された回答

the cyclist
the cyclist 2017 年 9 月 8 日
編集済み: the cyclist 2017 年 9 月 8 日
% Define array with some pretend data
S = rand(2496,10,13857);
D = rand(10,1);
% Take the weighted mean:
% (1) Reorient D to align with 2nd dimension
% (2) Take weighted mean along 2nd dimension
% (3) Reshape again to get rid of singleton 2nd dimension
wS = reshape(mean(S.*reshape(D,[1,10,1]),2),[2496,13857]);
You could have done similar things using permute instead of reshape.
Also, you could have used "squeeze" in step 3, but sometimes that command has unintended consequences, if some other dimension has length 1 unexpectedly.
  1 件のコメント
Meghan Rochford
Meghan Rochford 2017 年 9 月 8 日
Thank you for the help. It worked very well!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by