Calculate the mean of multiple 3D arrays layer by layer

11 ビュー (過去 30 日間)
AbioEngineer
AbioEngineer 2020 年 12 月 21 日
編集済み: Ameer Hamza 2020 年 12 月 21 日
Code is provided below but is inefficient. Is there a more elegant way?
Let's say I have 2 3D arrays: matrix1 and matrix2 both of the same dimension e.g. 10x10x3
How could I calculate the element-average between layer1 of matrix1 and matrix2 and do that for all 3 layers so the final outcome is another 10x10x3 matrix avgMat where avgMat(:, :, 1) is the mean of matrix1(:, :, 1) and matrix2(:, :, 1)?
m1 = cat(3,matrix1(:,:,1),matrix2(:,:,1));
m1=mean(m1,3);
m2 = cat(3,matrix1(:,:,2),matrix2(:,:,2));
m2=mean(m2,3);
m3 = cat(3,matrix1(:,:,3),matrix2(:,:,3));
m3=mean(m3,3);
avgMat=cat(3,m1, m2, m3)

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 21 日
編集済み: Ameer Hamza 2020 年 12 月 21 日
I am not sure why you are concerned about the layers. You are just taking an element-wise average of two matrices. Talking about layers is just making your problem more confusing. The following is equivalent to your code.
avgMat = (matrix1+matrix2)/2
Just in case you are intentionally looking for something confusing
avgMat2 = mean(cat(4, matrix1, matrix2), 4)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by